← Amazon Interview Insights

Amazon·Software Engineer·Online Assessment (OA)·Intermediate

Intermediate
Jul 2026

Summary

Amazon SWE coding round, one graph traversal problem. Pretty standard DFS stuff but the setup tripped me up a bit.

Questions Asked (1)

Q1

Given a directed acyclic graph with n nodes and a list of edges, write a program to find all possible paths starting from a given source node.

Algorithms & Data Structures
Author's notes

Went with DFS and backtracking.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clarifying the problem: confirm whether the graph is guaranteed acyclic, whether paths can share nodes, and what output format is expected. Then propose a DFS-based backtracking solution that explores all paths from the source, using a visited set to avoid cycles (though DAG ensures no cycles, it's good practice). Discuss time complexity as O(2^n) in the worst case due to exponential number of paths.

Pro tip: Mention that for very large graphs, enumerating all paths is inherently exponential, so you should ask if the interviewer expects an optimized approach for a specific subset (e.g., shortest paths) or if the problem is purely about enumeration. This shows awareness of practical constraints.

1. Clarify requirements and edge cases

Ask about input format, whether the graph is guaranteed acyclic, if paths can repeat nodes, and if the source is always valid. Confirm output should be a list of paths.

2. Choose the right algorithm

Use DFS with backtracking to explore all paths. Since it's a DAG, no cycle detection is needed, but you can still use a visited set to avoid revisiting nodes in the current path.

3. Implement the DFS

Write a recursive function that takes the current node and the path so far. If the current node has no outgoing edges, add the path to the result. Otherwise, iterate over neighbors and recurse.

4. Analyze complexity and optimize if needed

State that time complexity is O(2^n) in the worst case (exponential number of paths) and space is O(n) for recursion depth plus output storage. Discuss potential optimizations like pruning if only paths to a specific target are needed.

5. Test with examples

Walk through a small example (e.g., 4 nodes) to verify correctness, including edge cases like source with no outgoing edges or single-node graph.

Key Points to Mention

  • DFS with backtracking is the standard approach for enumerating all paths in a DAG.
  • Time complexity is exponential (O(2^n)) because the number of paths can be exponential in the worst case.
  • Use a visited set to avoid cycles, even though DAG guarantees no cycles, it's good practice and handles general graphs.
  • Clarify if the problem expects paths to any node or only to a specific target; if target is given, you can prune.
  • Consider iterative DFS with an explicit stack if recursion depth is a concern.
  • Discuss output format: list of lists or list of strings, and how to handle large outputs.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.