Code Room
CodingHardcod-g013
Subject Topological sortLevel Senior–Staff~35 minCommon in Algorithms & data structures interviewsIndustries Software development

Question

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.

Vibe coding: describe the solution in plain language (or narrate it) and the coach grades your approach. Generating runnable code from your description is coming next.

Run or narrate your approach, then ask the coach.