Determine Maximum Path Length in Directed Acyclic Graph
Company: Amazon
Role: Data Scientist
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
Overview: This question evaluates proficiency in graph algorithms and algorithmic reasoning, focusing on properties of directed acyclic graphs, longest-path concepts, and complexity analysis related to dependency chains.
Constraints
- 0 <= number of nodes n <= 100000
- 0 <= number of edges m <= 200000
- Graph is directed and acyclic (DAG); no self-loops or parallel edges needed
- Node labels are unique non-empty strings
- Nodes may appear only in neighbor lists; treat them as having empty adjacency lists
- Return 0 for an empty graph
- Aim for O(n + m) time and O(n + m) space
Hints
- Use either DFS with memoization or a topological ordering to avoid recomputation.
- In topological order, let dp[u] be the longest path (in nodes) ending at u; relax edges u->v with dp[v] = max(dp[v], dp[u] + 1).
- Be sure to include nodes that only appear in neighbor lists and treat missing keys as empty adjacency lists.