KatInterview

Deadline-Priority Ordering of Dependent Jobs

Heaps, Sorting & SearchAuto-graded · 17 test cases
MediumAuto-gradedHeaps, Sorting & Searchtopological-sortheapgraph

Last updated

Produce an execution order for jobs with prerequisites by always running the ready job with the earliest deadline, breaking ties by id, using topological sort with a heap.

Deadline-Priority Ordering of Dependent Jobs

A job runner receives a list of jobs. Each job is a dictionary with these keys:

  • task_id: a unique string identifying the job.
  • deadline: an integer.
  • prerequisites: an optional list of task_id strings naming jobs that must finish before this job can start. If the key is missing or the list is empty, the job has no prerequisites.

The prerequisite relationships form a directed acyclic graph, and every prerequisite id refers to a job that appears in the input.

Task

Simulate the runner and return the order in which the jobs execute. Jobs run one at a time. At each step:

  1. A job is ready if all of its prerequisites have already been executed.
  2. Among all ready jobs, run the one with the smallest deadline.
  3. If several ready jobs share the smallest deadline, run the one whose task_id is smallest in ordinary string comparison.
  4. Once a job runs, jobs that were waiting on it may become ready for the next step.

A job never runs before its prerequisites, even if its deadline is earlier than theirs.

Return the list of task_id values in execution order. If the input list is empty, return an empty list.

Constraints

  • The number of jobs is n, with 0 <= n <= 10^5.
  • Deadlines are integers and may be zero or negative.
  • task_id values are unique strings.
  • The total number of prerequisite entries is at most about 2 * 10^5.
  • The dependency graph has no cycles, and all prerequisite ids exist in the input.
  • A prerequisite list may contain the same id more than once; this must not break the result.
  • Aim for roughly O(n log n + E) time, where E is the number of prerequisite entries.

Examples

Example 1

Input: order_jobs_by_deadline([{'task_id': 'a', 'deadline': 3, 'prerequisites': []}, {'task_id': 'b', 'deadline': 1, 'prerequisites': []}, {'task_id': 'c', 'deadline': 2, 'prerequisites': []}])

Output: ['b', 'c', 'a']

Example 2

Input: order_jobs_by_deadline([{'task_id': 'compile', 'deadline': 5, 'prerequisites': ['fetch']}, {'task_id': 'fetch', 'deadline': 1, 'prerequisites': []}, {'task_id': 'test', 'deadline': 4, 'prerequisites': ['compile']}, {'task_id': 'docs', 'deadline': 2, 'prerequisites': []}])

Output: ['fetch', 'docs', 'compile', 'test']

Example 3

Input: order_jobs_by_deadline([{'task_id': 'a', 'deadline': 1, 'prerequisites': []}, {'task_id': 'b', 'deadline': 1, 'prerequisites': []}, {'task_id': 'c', 'deadline': 1, 'prerequisites': ['a']}, {'task_id': 'd', 'deadline': 0, 'prerequisites': ['b']}])

Output: ['a', 'b', 'd', 'c']

Hints

Hint 1

A job becomes ready exactly when every prerequisite has run. Track, for each job, how many prerequisites are still unfinished.

Hint 2

Store reverse edges (prerequisite -> jobs that depend on it) so that finishing a job lets you update only its dependents instead of rescanning everything.

Hint 3

Keep the currently ready jobs in a min-heap keyed by the tuple (deadline, task_id); the tuple comparison gives the required tie-break for free.