← Amazon Interview Insights

Amazon·Software Engineer·Technical Phone Screen·Intermediate

IntermediatePrefer not to say
Apr 2026

Summary

Amazon SWE coding round, one graph problem the whole time. Pretty standard stuff if you've seen it before, but the details matter more than you'd think.

Questions Asked (1)

Q1

Given a reference to a node in a connected undirected graph, return a deep copy of the entire graph. Each node has an integer value and a list of neighbors.

Algorithms & Data Structures
Author's notes

I knew the answer involved a hash map from old nodes to new ones, but I fumbled the explanation of why you need it.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use a depth-first search (DFS) or breadth-first search (BFS) to traverse the graph while maintaining a hash map from original nodes to their copies. For each visited node, create a copy if it doesn't exist, then recursively (or iteratively) copy its neighbors and add them to the copy's neighbor list. Return the copy of the starting node.

Pro tip: Clarify that the graph is connected and undirected, and mention that you'll handle cycles using the hash map to avoid infinite recursion. Also, discuss edge cases like a single node with no neighbors or an empty graph (though the problem states connected, so at least one node).

1. Understand the problem and clarify constraints

Confirm that the graph is connected and undirected, and that each node has a unique value (or not, but typically values are unique). Ask if the graph can have cycles or self-loops, and whether the input node is guaranteed to be non-null.

2. Choose traversal method and data structure

Decide between DFS (recursive or iterative) and BFS. Use a hash map (dictionary) to map original nodes to their copies, which also serves as the visited set to handle cycles.

3. Implement the copy logic

Start from the given node. If the node is already in the hash map, return its copy. Otherwise, create a new node with the same value, add it to the map, then recursively copy each neighbor and append the copies to the new node's neighbors list.

4. Test with examples and edge cases

Walk through a simple graph (e.g., two nodes connected) and a graph with a cycle. Verify that the deep copy is independent (modifying the copy does not affect the original). Also consider a single node with no neighbors.

5. Analyze time and space complexity

State that the time complexity is O(N + E) where N is the number of nodes and E is the number of edges, since each node and edge is visited once. Space complexity is O(N) for the hash map and recursion stack (or queue for BFS).

Key Points to Mention

  • Use a hash map to map original nodes to their copies, which prevents infinite loops in cyclic graphs.
  • Traverse the graph using DFS or BFS; both are acceptable, but DFS is often more concise.
  • Create the copy of a node before recursing on its neighbors to handle cycles correctly.
  • Ensure the deep copy is independent: new nodes and new neighbor lists, not references to original nodes.
  • Discuss time and space complexity: O(N + E) time, O(N) space.
  • Consider edge cases: single node, self-loop, and large graph (recursion depth might be an issue, so iterative DFS or BFS could be safer).

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