This is a classic DAG longest-path problem but the twist is you also have to reconstruct the path, not just return the length.
Model the tasks as a DAG and use topological sorting to compute the longest path to each node, tracking predecessors to reconstruct the critical path. For multiple test graphs, process each independently, ensuring efficient O(V+E) time per graph.
Pro tip: Clarify edge cases upfront (e.g., empty graph, disconnected components, multiple critical paths) and discuss how to handle them—this shows thoroughness and prevents ambiguity during implementation.
Confirm that the graph is a DAG, tasks have durations, and dependencies are directed edges. Ask about output format for multiple graphs and tie-breaking rules for multiple critical paths.
Use topological sort to process nodes in dependency order. For each node, compute the longest path ending at that node by taking the maximum over its predecessors, and store the predecessor that gives the maximum.
Write a function that takes a graph and returns the critical path length and sequence. Loop over all test graphs, applying the function to each, and collect results.
After computing longest distances, find the node with the maximum distance (end of critical path). Backtrack using stored predecessors to build the sequence of tasks from start to end.
Test with simple graphs (single node, chain, diamond) and edge cases (empty graph, multiple sources/sinks). Verify that the path length equals the sum of durations along the path.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.