KatInterview

Latest-Wins Priority Job Runner Simulation

Data Structure DesignAuto-graded · 18 test cases
MediumAuto-gradedData Structure Designheaplazy-deletionsimulation

Last updated

Simulate a job runner where resubmitting an ID overrides its earlier entry, testing heap-based lazy deletion with tie-breaking and run-once rules.

Latest-Wins Priority Job Runner

You are simulating a job runner that holds pending jobs and runs them one at a time. Each job has an integer id, an integer priority, and an integer timestamp. You receive a sequence of commands and must report what each run command returns.

Commands

The two input lists have the same length. commands[i] is the name of the i-th command, and params[i] holds its arguments.

  • "addTask" with params[i] = [task_id, priority, timestamp] submits a job.
    • If task_id has never been submitted, it becomes pending with the given priority and timestamp.
    • If task_id is already pending, the new priority and timestamp replace the old ones. The most recent submission wins, even if its priority is lower than before.
    • If task_id has already been run, the submission is ignored completely.
  • "executeTask" with params[i] = [] picks the next pending job, removes it, and returns its id. After this, the id counts as run and can never run again.

Choosing the next job

Among the pending jobs, choose the one that has:

  1. the highest priority;
  2. if priorities tie, the smallest timestamp;
  3. if both tie, the smallest task id.

Only each job's current (latest) priority and timestamp are considered.

If no job is pending, executeTask returns None (null in the sample outputs).

Output

Return a list with one entry per "executeTask" command, in order. Each entry is the returned id or None. "addTask" commands add nothing to the output.

Constraints

  • 1 <= len(commands) <= 10^5, and len(params) == len(commands).
  • Every command is either "addTask" or "executeTask".
  • Task ids, priorities and timestamps are integers and may be negative.
  • The same id may be submitted many times.
  • The solution should run in about O(n log n) overall.

Examples

Example 1

Input: simulate_priority_runner(['addTask', 'addTask', 'addTask', 'executeTask', 'executeTask'], [[1, 10, 100], [2, 20, 50], [1, 15, 120], [], []])

Output: [2, 1]

Example 2

Input: simulate_priority_runner(['addTask', 'addTask', 'executeTask', 'addTask', 'executeTask', 'executeTask'], [[7, 5, 10], [3, 5, 10], [], [7, 100, 1], [], []])

Output: [3, 7, None]

Hints

Hint 1

Python's heapq is a min-heap. How can you encode 'highest priority first, then earliest timestamp, then smallest id' as a tuple that sorts ascending?

Hint 2

A heap cannot cheaply update or remove an arbitrary entry. Instead of editing the old entry, push a new one and remember which entry is currently valid for each id.

Hint 3

Give each submission a version number per id. When you pop, discard entries whose version is not the latest one stored for that id, and keep a set of ids that already ran so later submissions are ignored.