Amazon·Software Engineer·Technical Phone Screen
- Given a directed acyclic graph with N nodes and M edges, a source node s, and an integer L, design a 2D DP solution dp[u][k] that counts the number of distinct paths from s to u using exactly k edges. Walk through the state definition, base cases, transition, and iteration order.
- What are the time and space complexities of your DP solution, and how would you reduce space usage using rolling arrays?
- How would you adapt this DP framework for weighted edges, for example to find the shortest or longest path using at most or exactly k edges?
- How would you modify the solution to count paths specifically to a target node t rather than all nodes?
“I knew topological sort was involved but fumbled the base case explanation for like two minutes.”