Code RoomLongest path in DAG
HardPrep Room Coding #73

Longest path in DAG

CodingAlgorithms & data structuresSenior–Staff~35 min

You are given a DAG of n nodes (0..n-1) with weighted directed edges [u, v, w], w >= 0, representing task durations along dependencies. Return the length of the longest path (maximum total weight) in the DAG over all start/end choices. If the input contains a cycle (so 'longest path' is unbounded/undefined), return -1. A single node with no edges contributes a longest path of 0.

Implement
dag_longest_path(n: int, edges: list[list[int]]) → int
Examples
in[5,[[0,1,3],[0,2,2],[1,3,4],[2,3,1],[3,4,2]]]out9
What a strong answer looks like

State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.

0:00 of about 35 min
InputExpectedGot
[5,[[0,1,3],[0,2,2],[1,3,4],[2,3,1],[3,4,2]]]9not run yetsample