Find the Critical Path
Company: Modular
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: hard
Interview Round: Technical Screen
Quick Answer: This question evaluates competency in graph algorithms and project scheduling, specifically reasoning about directed acyclic graphs, task dependencies, and accumulation of task durations to identify a critical path.
Constraints
- 0 <= number of tasks <= 2000
- 0 <= total number of prerequisite links <= 10000
- Each task name is a unique string
- The dependency graph is a DAG
- 1 <= duration <= 10^6 for each task
Examples
Input: []
Expected Output: (0, [])
Explanation: There are no tasks, so the critical path has duration 0 and an empty path.
Input: [('A', [], 3), ('B', ['A'], 2), ('C', ['A'], 5)]
Expected Output: (8, ['A', 'C'])
Explanation: Path A -> B has total duration 5, while A -> C has total duration 8. The critical path is ['A', 'C'].
Input: [('A', [], 2), ('B', ['A'], 4), ('C', ['B'], 3), ('D', ['C'], 6)]
Expected Output: (15, ['A', 'B', 'C', 'D'])
Explanation: There is a single chain A -> B -> C -> D, and its total duration is 2 + 4 + 3 + 6 = 15.
Input: [('Task1', [], 7)]
Expected Output: (7, ['Task1'])
Explanation: With only one task, that task itself is the critical path.
Input: [('A', [], 3), ('B', [], 3), ('C', ['A'], 2), ('D', ['B'], 2)]
Expected Output: (5, ['A', 'C'])
Explanation: Both ['A', 'C'] and ['B', 'D'] have total duration 5. The lexicographically smaller path is ['A', 'C'].
Input: [('A', [], 2), ('B', [], 2), ('C', ['A'], 3), ('D', ['B'], 3), ('E', ['C', 'D'], 4)]
Expected Output: (9, ['A', 'C', 'E'])
Explanation: Task E depends on both C and D. The longest prerequisite chain to E is tied between ['A', 'C'] and ['B', 'D'], each totaling 5 before E. Adding E gives total 9, and the lexicographically smaller full path is ['A', 'C', 'E'].
Input: [('A', [], 1), ('B', ['A'], 2), ('C', ['A'], 2), ('D', ['B', 'C'], 3)]
Expected Output: (6, ['A', 'B', 'D'])
Explanation: Both paths ['A', 'B', 'D'] and ['A', 'C', 'D'] total 6. Since B < C, ['A', 'B', 'D'] is lexicographically smaller.
Hints
- For each task, its best finish time is its own duration plus the maximum best finish time among its prerequisites.
- Process the tasks in topological order so that all prerequisite results are known before computing a task.