Reported September 2026
Sunodynamic programming

Longest Path in a Directed Acyclic Graph

Reported by candidates from Suno's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Suno OA. Under 2s to a working solution.
Founder's read

Suno flagged this one in September 2026, and it's less scary than the title sounds. Longest path in a DAG is dynamic programming over a topological order, dressed up as a graph problem. If you've seen Kahn's algorithm once, you've seen 80% of it. The OA gives you n up to 100000 and 200000 edges, so the brute-force path search is dead on arrival. You need one linear pass. If your mind goes blank mid-assessment, StealthCoder runs invisibly on your desktop and hands you the solution while the proctor sees nothing.

The problem

You are given a directed acyclic graph with nodes 0 through n - 1 and directed edges [from, to]. Return the maximum number of edges in any directed path. An isolated node forms a path of length zero.

Function
longestDagPath(n: int, edges: int[][]) → int

Examples
Example 1
n = 5
edges = [[0,1],[0,2],[1,3],[2,3],[3,4]]
return = 3
A longest path such as 0 → 1 → 3 → 4 contains three edges.
Example 2
n = 4
edges = []
return = 0
Every node is isolated, so the longest path has zero edges.

Constraints
1 <= n <= 100000
0 <= edges.length <= 200000
The graph is acyclic and contains no duplicate edge.

Reported by candidates. Source: FastPrep

Pattern and pitfall

Here's what it really reduces to: for every node, longest path ending there equals 1 + the max of its predecessors' values, or 0 if it has none. Compute in-degrees, push all zero in-degree nodes into a queue, and pop them in topological order. When you pop u, for each edge u to v, set dp[v] = max(dp[v], dp[u] + 1), then decrement v's in-degree and enqueue it at zero. Track the global max of dp. That's O(n + e). The common pitfall is recursing with DFS on 100000 nodes and blowing the stack, or skipping memoization and going exponential. Another trap is returning the node count instead of the edge count. Isolated nodes return 0, so initialize dp to zeros. If you freeze during the live OA, StealthCoder is the hedge that gives you the Kahn plus DP template fast.

Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.

If this hits your live OA

You can drill Longest Path in a Directed Acyclic Graph cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge.

Get StealthCoder

Related leaked OAs

⏵ The honest play

You've seen the question. Make sure you actually pass Suno's OA.

Suno reuses patterns across OAs. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Longest Path in a Directed Acyclic Graph FAQ

What's the trick to Longest Path in a DAG?+

Process nodes in topological order and keep dp[v] as the longest path ending at v. For each edge u to v, update dp[v] = max(dp[v], dp[u] + 1). The answer is the max over all dp values. Since the graph is acyclic, one pass is enough.

How hard is this one really?+

Medium. If you know topological sort, it's a small extension. The difficulty is recognizing that DP and topological order go together. The code is about fifteen lines. Most failures come from stack overflow in recursive DFS or off-by-one on edges versus nodes.

Should I use DFS with memoization or Kahn's algorithm?+

Either works, but Kahn's is safer with n up to 100000 because it's iterative. Recursive DFS can hit stack limits in some languages. If you do use DFS, memoize per node and be ready to convert to iterative if the language is strict.

What edge cases break solutions?+

Empty edges list returns 0. A single node returns 0. Disconnected components need all zero in-degree nodes seeded in the queue, not just node 0. Also remember the answer counts edges, not nodes, so don't add one at the end.

How do I prepare for this in 48 hours?+

Write Kahn's algorithm from memory twice, then add the dp update line. Test it on the two examples, including the empty edges case. Know the complexity is O(n + e) and be ready to explain why a cycle can't exist here. That's enough for this problem.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Suno.

OA at Suno?
Invisible during screen share
Get it