Find the Critical Path
ModularSoftware Engineer · Technical ScreenLast updated
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.
You are given a project represented as a directed acyclic graph of tasks. Each task has:
- a unique task name
- a list of prerequisite task names
- a positive duration
The input format is:
TASK(name, dependencies, duration)
A task may start only after all of its dependencies have finished.
Write a function that, given a list of tasks, returns:
- the critical path value: the maximum total duration needed along any valid dependency chain
- one corresponding critical path: the ordered list of task names on that longest path
If multiple critical paths have the same total duration, returning any one of them is acceptable.
Example inputs:
graph1 = [
TASK("A", [], 3),
TASK("B", [], 5),
TASK("C", ["A"], 5),
]
graph2 = [
TASK("A", [], 2),
TASK("B", ["A"], 4),
TASK("C", ["B"], 3),
TASK("D", ["C"], 6),
]
graph3 = [
TASK("A", [], 3),
TASK("B", ["A"], 2),
TASK("C", ["A"], 7),
TASK("D", ["B", "C"], 4),
TASK("E", ["D"], 2),
]
graph4 = [
TASK("A", [], 3),
TASK("B", [], 8),
TASK("C", ["A"], 2),
TASK("D", ["C"], 5),
TASK("E", ["B"], 1),
TASK("F", ["D", "E"], 4),
]
graph5 = [
TASK("A", [], 1),
TASK("B", ["A"], 2),
TASK("C", ["A"], 5),
TASK("D", ["B"], 4),
TASK("E", ["C"], 3),
TASK("F", ["D", "E"], 3),
TASK("G", ["F"], 6),
TASK("H", ["C"], 2),
TASK("I", ["H"], 7),
TASK("J", ["G", "I"], 2),
]
graph6 = [
TASK("M", [], 4),
TASK("N", ["M"], 3),
TASK("O", ["N"], 2),
TASK("P", ["M"], 6),
TASK("Q", ["O", "P"], 5),
TASK("R", ["Q"], 4),
TASK("S", ["N"], 1),
TASK("T", ["S", "R"], 2),
]
Assume the dependency graph is valid and acyclic.
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.