KatInterview

Audit a Workflow Run Against Dependencies and Precedence Rules

Trees & GraphsAuto-graded · 22 test cases
MediumAuto-gradedTrees & Graphsgraphtopological-sortcycle-detection

Last updated

Check a run order against a task dependency graph, mandatory tasks and extra precedence pairs, while rejecting duplicates, unknown tasks and cyclic constraints.

A workflow engine records the order in which tasks were run. You must audit that record.

You are given:

  • workflow_graph: a dictionary mapping each task ID to a list of task IDs that depend on it (its downstream tasks). An entry u -> [v, ...] means u must run before each listed v.
  • run_order: a list of task IDs in the order they were run.
  • mandatory_tasks: a list of task IDs that must appear in run_order.
  • precedence_pairs: a list of pairs [u, v], each meaning u must run before v.

Return True if the run is valid, otherwise False.

Validity rules

A run is valid only if all of the following hold:

  1. No repeats. No task ID appears more than once in run_order.
  2. Known tasks only. The known tasks are every key of workflow_graph plus every task ID listed in any of its downstream lists. Every task in run_order must be a known task.
  3. Mandatory tasks present. Every task in mandatory_tasks appears in run_order.
  4. Constraint set. Build one combined set of constraints from both sources:
    • one constraint (u, v) for every edge u -> v in workflow_graph;
    • one constraint (u, v) for every pair in precedence_pairs.
  5. No self-constraints. No constraint may have u == v. This applies whether or not the task was run.
  6. Order respected. For every constraint (u, v) where both u and v appear in run_order, u must appear at an earlier position than v. If either task is absent from run_order, that constraint is skipped.
  7. Acyclic constraints. The directed graph formed by all constraints must contain no cycle. This is checked over all tasks mentioned in the constraints, including tasks that were not run.

If any rule is broken, return False.

Constraints

  • Task IDs are strings.
  • workflow_graph may contain tasks with empty downstream lists.
  • run_order, mandatory_tasks and precedence_pairs may be empty.
  • Each element of precedence_pairs is a list of exactly two task IDs.
  • Input sizes are small enough for a linear-time solution in the total size of the inputs.

Examples

Example 1

Input: audit_workflow_run({'A': ['B'], 'B': ['C'], 'C': []}, ['A', 'B', 'C'], ['A', 'B', 'C'], [['A', 'B'], ['B', 'C']])

Output: True

Example 2

Input: audit_workflow_run({'A': ['B'], 'B': ['C'], 'C': []}, ['B', 'A', 'C'], ['A', 'B', 'C'], [['A', 'B']])

Output: False

Example 3

Input: audit_workflow_run({'A': ['B'], 'B': ['C'], 'C': []}, ['A', 'C'], ['A', 'B', 'C'], [['A', 'B']])

Output: False

Example 4

Input: audit_workflow_run({'A': ['B'], 'B': ['A']}, ['A', 'B'], ['A', 'B'], [])

Output: False

Hints

Hint 1

Build a map from task to its index in run_order in one pass. The same pass can reject duplicates and unknown tasks.

Hint 2

Merge the graph edges and the precedence pairs into one list of constraints. Then each constraint is a constant-time index comparison, applied only when both tasks were run.

Hint 3

Cycle detection is independent of run_order. Run Kahn's algorithm (or a DFS with colors) on the merged constraint graph and fail if some node is never removed.