This is basically topological sort plus dynamic programming once you see it clearly, but I spent a few minutes fumbling around trying to think about it as a pure DFS before remembering you can just compute earliest completion times in topo order.
Model the problem as a longest path in a DAG, using topological sort and dynamic programming to compute the maximum duration to each task. Track predecessors to reconstruct one critical path, and return both the total duration and the ordered task list.
Pro tip: Clarify upfront whether the graph is guaranteed acyclic and whether multiple critical paths exist—then state that you'll return any one of them. This shows you think about edge cases and ambiguity before coding.
Confirm the graph is a DAG, tasks are uniquely named, and durations are non-negative. Ask whether to return any critical path if multiple exist.
Create an adjacency list from prerequisites to dependents and compute in-degrees for all tasks. This sets up topological sorting.
Process tasks in topological order, updating the maximum duration to each dependent as current duration plus edge weight. Store the predecessor that gave the maximum.
Find the task with the maximum total duration, then backtrack using stored predecessors to build the ordered list of task names.
Return the maximum duration and the reconstructed path. Mention time and space complexity: O(V + E) time and O(V + E) space.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.