Skip to content

ORIGINAL CODING QUESTION

Longest ordered dependency chain

Every directed edge points from a smaller vertex ID to a larger one. Return the longest path’s number of edges, allowing any start and end.

Open practice workspace

Original exercise · medium. Editorial role relevance does not imply a company has asked this question.

Input and output

n m, then m vertex pairs (u,v). Vertices are zero-based.

Constraints

1 ≤ n ≤ 30; 0 ≤ m ≤ 100; vertex IDs in [0,n). Every edge has u<v.

Example 1

Input

4 4 0 1 1 2 2 3 0 3

Output

3

The four-vertex chain has three edges.

Example 2

Input

3 0

Output

0

All paths have zero edges.

Explain your approach

Explain your time and auxiliary space complexity. Which input makes your algorithm work hardest?

Private grading inputs, expected outputs and canonical reference implementations are withheld. Authored expected outputs were cross-checked with independent reference oracles; compiler/runtime behavior still needs a verified execution provider.

Hint 1

Vertex IDs already provide a topological order.

Hint 2

Propagate the best ending-path length along each edge.

Hint 3

Take the greatest ending length.

Practice this question

Save your work privately, explain decisions and return to your revision queue.

Open practice workspaceBrowse the question library →