KatInterview

Deadline-First Job Queue with Prerequisite Gating

Data Structure DesignAuto-graded · 17 test cases
MediumAuto-gradedData Structure Designheaptopological-sortgraph

Last updated

Simulate a job queue where jobs unlock after their prerequisites are consumed, always serving the earliest-deadline unlocked job; tests heaps, dependency counting, and tie-breaking.

You are simulating a job queue in which some jobs cannot be served until other jobs have been served first. You are given a list of operations and a matching list of arguments, and you must return the result of each operation in order.

Operations

Each entry of operations is one of two strings:

  • "AddTasks": the corresponding entry of arguments is a list whose first element is a list of task dictionaries. Add all of those tasks to the system, in the order given. The result recorded for this operation is None.
  • "ConsumeTask": the corresponding entry of arguments is an empty list. Remove one task from the system and record that task's full dictionary as the result. If there is no eligible task, record the string "NO_TASK" instead.

Task dictionaries

Each task is a dictionary with these keys:

  • id: a unique identifier for the task.
  • ddl: an integer deadline. A smaller value means an earlier deadline.
  • subTasks: a list of ids of prerequisite tasks. This key may be missing, which is the same as an empty list. The list may contain the same id more than once, and repeats count as a single prerequisite.

Rules

  1. A task is eligible once every one of its prerequisites has already been consumed. A task with no prerequisites is eligible as soon as it is added.
  2. A prerequisite may be added in a later AddTasks call than the task that depends on it. Until that prerequisite has been added and consumed, the dependent task is not eligible. A prerequisite that is never added blocks its dependents forever.
  3. A prerequisite that was already consumed before the dependent task was added counts as satisfied.
  4. ConsumeTask selects the eligible task with the smallest ddl, removes it, and returns it.
  5. If several eligible tasks share the smallest ddl, choose the one that became eligible earliest. A task becomes eligible at the moment it is added (if it has no unmet prerequisites) or at the moment its last unmet prerequisite is consumed. Tasks that become eligible during the same AddTasks call are ordered by their position in the added list. Tasks unlocked by a single consume are ordered by the order in which they were registered as waiting on the consumed task.
  6. Task ids are unique and the dependency relationships contain no cycles.

Return a list with one result per operation, in the same order as operations.

Constraints

  • len(operations) == len(arguments).
  • Every operation is "AddTasks" or "ConsumeTask".
  • Task ids are unique across the whole run, and the dependencies are acyclic.
  • ddl values are integers.
  • Aim for roughly logarithmic cost per consume rather than rescanning every pending task.

Examples

Example 1

Input: simulate_job_queue(['AddTasks', 'ConsumeTask', 'ConsumeTask', 'ConsumeTask'], [[[{'id': 'A', 'ddl': 5, 'subTasks': []}, {'id': 'B', 'ddl': 2, 'subTasks': ['A']}, {'id': 'C', 'ddl': 3, 'subTasks': []}]], [], [], []])

Output: [None, {'id': 'C', 'ddl': 3, 'subTasks': []}, {'id': 'A', 'ddl': 5, 'subTasks': []}, {'id': 'B', 'ddl': 2, 'subTasks': ['A']}]

Example 2

Input: simulate_job_queue(['AddTasks', 'AddTasks', 'ConsumeTask', 'ConsumeTask'], [[[{'id': 'A', 'ddl': 4, 'subTasks': []}]], [[{'id': 'B', 'ddl': 1, 'subTasks': ['A']}]], [], []])

Output: [None, None, {'id': 'A', 'ddl': 4, 'subTasks': []}, {'id': 'B', 'ddl': 1, 'subTasks': ['A']}]

Hints

Hint 1

Think of each pending task as either blocked or ready. Only ready tasks ever need to be compared by deadline, so keep them in a min-heap.

Hint 2

For every task track how many distinct prerequisites are still unconsumed, and keep a reverse map from a prerequisite id to the tasks waiting on it. A prerequisite may not have been added yet, so register the reverse link anyway unless it is already consumed.

Hint 3

When a task is consumed, decrement the counters of its waiting tasks and push those reaching zero into the heap. Use an increasing counter in the heap entries (deadline, counter, id) to break deadline ties by when the task became eligible.