KatInterview

Priority Job Scheduler with Cancellation

Data Structure DesignAuto-graded · 23 test cases
MediumAuto-gradedData Structure Designheappriority-queuelazy-deletion

Last updated

Simulate a scheduler that adds, cancels, and runs jobs by priority with FIFO tie-breaking, using lazy deletion on a heap so each operation stays logarithmic.

Priority Job Scheduler with Cancellation

You are simulating a job scheduler that holds a set of pending jobs. Each job has a unique integer id and an integer priority. The scheduler processes a sequence of commands and reports a result for each one.

Commands

The input consists of two parallel lists. commands[i] is the name of the i-th command and params[i] is the list of integer parameters for it.

CommandParametersEffectResult
"add"[job_id, priority]Registers a pending job with this id and priority. If a job with the same id is already pending, it is replaced by the new one (new priority, and it counts as newly arrived for tie-breaking).None
"remove"[job_id]Cancels the pending job with this id. If there is no such pending job (never added, already cancelled, or already run), nothing happens.None
"executeNext"[]Picks the best pending job, removes it from the scheduler, and returns its id. If nothing is pending, returns None.job id or None

Any command name other than "add" and "remove" should be treated as "executeNext".

Ordering rule

The best pending job is the one with the highest priority. If several pending jobs share the highest priority, the one that was added earliest is chosen. Arrival order is determined by the order of add commands: every add counts as a new arrival, including one that replaces an existing pending job with the same id or re-adds an id that was previously cancelled or run.

Task

Implement a function that processes all commands in order and returns a list with one result per command, in the same order.

Your solution should not scan all pending jobs when executing the next one; aim for O(log n) time per command (amortized is acceptable).

Constraints

  • len(commands) == len(params)
  • Job ids and priorities are integers; priorities may be zero or negative.
  • Cancelling or executing a job that is not pending is not an error.
  • executeNext on an empty scheduler returns None.
  • A job id can be added, cancelled, and added again any number of times.

Examples

Example 1

Input: run_scheduler(['add', 'add', 'executeNext', 'executeNext'], [[1, 5], [2, 5], [], []])

Output: [None, None, 1, 2]

Example 2

Input: run_scheduler(['add', 'add', 'remove', 'executeNext', 'executeNext'], [[10, 3], [11, 8], [11], [], []])

Output: [None, None, None, 10, None]

Example 3

Input: run_scheduler(['executeNext', 'remove', 'add', 'remove', 'executeNext'], [[], [99], [7, 1], [7], []])

Output: [None, None, None, None, None]

Hints

Hint 1

A binary heap gives fast access to the best job, but it cannot remove an arbitrary buried element cheaply. Think about deferring the removal.

Hint 2

Store entries as (-priority, arrival_counter, job_id) so that Python's min-heap yields the highest priority first and the earliest arrival wins ties. Keep a dictionary of the currently valid entry for each job id.

Hint 3

On cancel, just delete the id from the dictionary. When executing, pop heap entries until one equals the dictionary's current entry for its id; compare the whole entry, not just whether the id exists, so re-added ids do not resurrect old entries.