487. Design Job Scheduler with Waiting Queue
Design Job Scheduler with Waiting Queue

Build an in-memory scheduler to schedule jobs on multiple machines. Each job has an earliest start time and a duration in seconds. Each machine handles one job at a time, while different machines may work during the same time interval. A job stays QUEUED until it starts.

Method Signatures

JobScheduler(List<String> machineIds)
  • Creates an empty scheduler at time 0 with the supplied unique machine IDs.
  • Machines remain registered, are initially available, and can run any job. Input list order does not affect assignment.
boolean scheduleJob(String jobId, long scheduledTimeInSeconds, long durationInSeconds, long currentTimeInSeconds)
  • Adds a one-time job in QUEUED state. Returns true when accepted.
  • scheduledTimeInSeconds is the earliest allowed start time; durationInSeconds is the time spent running.
  • Returns false if the job ID was already accepted, including completed or cancelled jobs. No new job is added.
  • An eligible new job starts immediately if a machine is available. Past scheduled times are allowed, but the job cannot start before submission.
boolean cancelJob(String jobId, long currentTimeInSeconds)
  • Returns true and changes a still-queued job to CANCELLED. It will never execute.
  • Returns false for an unknown, running, completed, or already cancelled job.
String getJobStatus(String jobId, long currentTimeInSeconds)
  • Returns "QUEUED", "RUNNING,machineId", "COMPLETED,machineId", "CANCELLED", or "UNKNOWN".
  • Replace machineId with the assigned machine ID, without adding spaces around the comma. Completed jobs retain that ID after machine reuse. An unrecognized job ID returns "UNKNOWN".

Time and Scheduling Rules

  • All calls are sequential. Across all methods on one instance, currentTimeInSeconds never decreases. Equal timestamps are allowed and calls are processed in order.
  • Before each requested operation, process every pending start and completion at or before its timestamp for previously submitted jobs. Events occur at their actual times, including during large time jumps. This also applies when the operation returns false or "UNKNOWN".
  • At each event time, first complete all finishing jobs and release their machines. Then assign eligible queued jobs. Finally, perform the requested operation when its timestamp is reached.
  • A queued job is eligible once its scheduled time is reached. Whenever machines are free, select eligible jobs by earliest scheduled time, breaking ties by lexicographically smallest job ID. Assign each selected job to the lexicographically smallest free machine. Continue until no assignment is possible.
  • Selection considers jobs currently queued. Later submissions do not interrupt running jobs. Each job runs once at most, entirely on one machine.
  • A job starting at time s completes at s + durationInSeconds. It is running during s ≤ currentTimeInSeconds < s + durationInSeconds. Another job may start on that machine exactly when it completes.
  • A job that starts at a call's timestamp is already running before cancellation is attempted. Completed and cancelled jobs remain available for status queries.

Constraints

  • 1 ≤ machineIds.size() ≤ 1,000; machine IDs are unique.
  • Job and machine IDs are nonblank, case-sensitive strings of 1 to 100 characters without commas. IDs are stored as supplied; comparisons use Java's String.compareTo order.
  • 0 ≤ scheduledTimeInSeconds, currentTimeInSeconds ≤ 1,000,000,000,000.
  • 1 ≤ durationInSeconds ≤ 1,000,000,000.
  • At most 100,000 constructor and method calls. No argument or list element is null.

Examples

Example 1: Queuing and Machine Reuse

Create JobScheduler(machineIds = List.of("node-9", "node-2")).

  1. scheduleJob(jobId = "zip", scheduledTimeInSeconds = 4, durationInSeconds = 6, currentTimeInSeconds = 0) returns true.
  2. scheduleJob(jobId = "check", scheduledTimeInSeconds = 4, durationInSeconds = 4, currentTimeInSeconds = 0) returns true.
  3. scheduleJob(jobId = "mail", scheduledTimeInSeconds = 5, durationInSeconds = 3, currentTimeInSeconds = 0) returns true.
  4. getJobStatus(jobId = "check", currentTimeInSeconds = 6) returns "RUNNING,node-2".
  5. getJobStatus(jobId = "zip", currentTimeInSeconds = 6) returns "RUNNING,node-9".
  6. getJobStatus(jobId = "mail", currentTimeInSeconds = 6) returns "QUEUED".
  7. getJobStatus(jobId = "check", currentTimeInSeconds = 8) returns "COMPLETED,node-2".
  8. getJobStatus(jobId = "mail", currentTimeInSeconds = 8) returns "RUNNING,node-2".
  9. getJobStatus(jobId = "mail", currentTimeInSeconds = 11) returns "COMPLETED,node-2".

At time 4, check wins the job-ID tie and starts on node-2; zip starts on node-9. Job mail waits until time 8, then runs for three seconds.

Example 2: Cancellation and a Past Scheduled Time

Create a new JobScheduler(machineIds = List.of("unit-4")).

  1. scheduleJob(jobId = "scan", scheduledTimeInSeconds = 3, durationInSeconds = 4, currentTimeInSeconds = 0) returns true.
  2. scheduleJob(jobId = "export", scheduledTimeInSeconds = 4, durationInSeconds = 2, currentTimeInSeconds = 0) returns true.
  3. cancelJob(jobId = "export", currentTimeInSeconds = 5) returns true.
  4. getJobStatus(jobId = "export", currentTimeInSeconds = 5) returns "CANCELLED".
  5. scheduleJob(jobId = "refresh", scheduledTimeInSeconds = 1, durationInSeconds = 2, currentTimeInSeconds = 7) returns true.
  6. cancelJob(jobId = "refresh", currentTimeInSeconds = 7) returns false.
  7. getJobStatus(jobId = "scan", currentTimeInSeconds = 7) returns "COMPLETED,unit-4".
  8. getJobStatus(jobId = "refresh", currentTimeInSeconds = 9) returns "COMPLETED,unit-4".

Job scan runs from time 3 to 7. Job export is cancelled while queued. Job refresh starts at submission time 7, so it cannot be cancelled and completes at time 9.



Please use Laptop/Desktop or any other large screen to add/edit code.