Job Scheduler with Delays and Cancellation
FigmaOnsiteMedium
A scheduler runs jobs on a fixed pool of workers identical workers. Time is an integer clock passed as now with every call, and it never decreases. Each worker runs one job at a time and a job runs without interruption: a job that starts at time s with duration d is running at times s through s + d - 1 and done from s + d onward. A free worker immediately takes the next waiting job, and a worker that finishes at time t can start another job at t.
Part 1: Immediate jobs
Implement submit(now, job_id, duration), which submits a job that should run as soon as possible. Jobs start in the order they were submitted, and submit returns nothing.
Implement status(now, job_id), which returns "queued" while the job waits for a worker, "running", "done", or None if no job with that id was submitted.
- Example 1
- init
[2]- operations
[["submit",[0,"a",5]],["submit",[0,"b",3]],["submit",[1,"c",2]],["status",[1,"a"]],["status",[1,"c"]],["status",[3,"b"]],["status",[3,"c"]],["status",[5,"a"]],["status",[5,"c"]],["status",[5,"zzz"]]]- Output
[null,null,null,"running","queued","done","running","done","done",null]
Why: Jobs
aandbstart at time 0 on the two workers;carrives at 1 and queues.bfinishes at 3, socstarts at 3 and finishes at 5, andafinishes at 5. An id that was never submitted has statusNone.
Constraints
1 <= workers <= 1001 <= len(operations) <= 10^40 <= now <= 10^9, andnownever decreases between calls1 <= duration <= 10^91 <= len(job_id) <= 10'a' <= job_id[i] <= 'z'job_idvalues are pairwise distinct