FastPrepLongest Path in a Directed Acyclic Graph
Problems/Longest Path in a Directed Acyclic Graph

Longest Path in a Directed Acyclic Graph

Suno logoSuno● MediumFULLTIMEPHONE SCREEN
QuestionFunctionExamplesConstraints

Problem statement

Source note: A verified Suno senior software engineer candidate report published August 28, 2026 names a DAG longest-path coding round. The node numbering and edge-count return convention are explicit deterministic practice choices.

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 = 5edges = [[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 = 4edges = []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.

More Suno problems

  • Referral Credits After a First SongPHONE SCREEN
See Suno hiring insights
Saved
class Solution {
    public int longestDagPath(int n, int[][] edges) {
      // Write your code here.
    }

}
Checking account…