KatInterview

Job Scheduling with Cycle Check and Downstream Rerun Set

Trees & GraphsAuto-graded · 24 test cases
MediumAuto-gradedTrees & Graphstopological-sortgraphbfs

Last updated

Topologically sort jobs with cycle detection, then use graph reachability to find all jobs affected by changed ones and list them in valid order.

You manage a build pipeline made of named jobs. Some jobs need other jobs to finish first. You must (1) produce a valid run order for all jobs, and (2) after some jobs are modified, work out which jobs must run again and in what order.

Task

Implement order_jobs_and_reruns(jobs, requires, modified) returning a list of two lists:

  1. full_order: an ordering of every job in which each job appears after all of its prerequisites.
  2. rerun_order: an ordering of only the jobs that must rerun, also respecting prerequisites.

Input

  • jobs: a list of unique, non-empty job names.
  • requires: a list of pairs [job, prerequisite], meaning prerequisite must finish before job starts. Every name in a pair appears in jobs. Duplicate pairs have no additional meaning. A pair where job == prerequisite is a cycle.
  • modified: a list of job names, all present in jobs. Names may repeat.

Rules

  • A modified job must rerun. So must every job that depends on it directly or indirectly (any job reachable by following "is a prerequisite of" links from a modified job).
  • Prerequisites that are not affected are assumed to have already completed successfully and are not rerun.
  • If the dependency graph contains a cycle anywhere (even among jobs unrelated to the modified ones), return [["CYCLE"], []]. The name CYCLE is never used as a job name.
  • If jobs is empty, return [[], []].

Deterministic output

Any valid ordering is logically correct, but to make the output reproducible, build full_order with the following procedure:

  1. Compute, for each job, the number of entries in requires that list it as the dependent (counting duplicates).
  2. Start a FIFO queue containing all jobs with count 0, in the order they appear in jobs.
  3. Repeatedly remove the front job and append it to full_order. Then go through the entries of requires in their given order. For each entry whose prerequisite is this job, decrease the dependent's count by one, and enqueue it when its count reaches 0.
  4. If full_order ends up shorter than jobs, a cycle exists.

rerun_order is the affected jobs listed in the same relative order as in full_order.

Constraints

  • 0 <= len(jobs) <= 10^5
  • 0 <= len(requires) <= 2 * 10^5
  • 0 <= len(modified) <= 10^5
  • Job names are unique, non-empty strings.
  • Aim for time and memory linear in the number of jobs plus dependencies.

Examples

Example 1

Input: order_jobs_and_reruns(['build', 'test', 'package', 'docs'], [['test', 'build'], ['package', 'test']], ['test'])

Output: [['build', 'docs', 'test', 'package'], ['test', 'package']]

Example 2

Input: order_jobs_and_reruns(['a', 'b', 'c'], [['b', 'a'], ['c', 'b']], ['a'])

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

Example 3

Input: order_jobs_and_reruns(['a', 'b', 'c'], [['b', 'a'], ['a', 'b']], ['a'])

Output: [['CYCLE'], []]

Example 4

Input: order_jobs_and_reruns([], [], [])

Output: [[], []]

Hints

Hint 1

Treat each pair as a directed edge from prerequisite to dependent. Keep an adjacency list and a count of unmet prerequisites per job.

Hint 2

Kahn's algorithm gives a valid order, and if it cannot place every job, the graph has a cycle.

Hint 3

Affected jobs are everything reachable from the modified jobs along the prerequisite-to-dependent edges. A BFS or DFS with a visited set finds them, and repeated names are harmless.

Hint 4

You do not need to sort the affected jobs again. Filtering the full valid order down to the affected set preserves all ordering constraints among them.