← Amazon Interview Insights

Amazon·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
Jun 2026

Summary

Amazon SWE interview with a grid pathfinding problem. Pretty standard Dijkstra territory but they pushed on implementation details and path reconstruction which is where things got interesting.

Questions Asked (2)

Q1

Given an m×n grid with nonnegative cell costs and some cells blocked, find the minimum cost path from a start cell to a target cell moving in four directions, or return -1 if no path exists. Walk through your approach and its time complexity.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

Jumped straight to Dijkstra which was right, but I fumbled explaining why BFS alone wouldn't work here.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Model the grid as a weighted graph where each cell is a node, edges connect adjacent unblocked cells with weight equal to the destination cell's cost, and use Dijkstra's algorithm to find the minimum cost path. If the target is unreachable, return -1. Analyze time complexity as O(mn log(mn)) with a binary heap.

Pro tip: Clarify whether the cost of the start cell is included; typically it's not, but confirming shows attention to detail. Also, mention that if all costs are equal, BFS suffices, but here costs vary so Dijkstra is needed.

1. Clarify problem details

Ask about edge cases: start or target blocked, start equals target, cost inclusion, and whether diagonal moves are allowed. Confirm that costs are nonnegative and that moving into a cell incurs its cost.

2. Choose algorithm

Since edge weights are nonnegative but not uniform, Dijkstra's algorithm is appropriate. If all costs were equal, BFS would work, but here we need a priority queue to always expand the lowest-cost path.

3. Implement Dijkstra

Initialize a distance matrix with infinity, set start distance to 0 (or start cost if included), and use a min-heap of (cost, row, col). While heap is not empty, pop the minimum, skip if already visited, and for each unblocked neighbor, relax the edge by adding the neighbor's cost.

4. Handle unreachable target

After the algorithm, if the target's distance remains infinity, return -1. Otherwise, return the distance.

5. Analyze complexity

Time complexity is O(mn log(mn)) because each cell is processed once and heap operations take logarithmic time. Space complexity is O(mn) for the distance matrix and heap.

Key Points to Mention

  • Dijkstra's algorithm for nonnegative weighted graphs
  • Priority queue (min-heap) implementation
  • Time complexity O(mn log(mn)) and space O(mn)
  • Handling blocked cells and unreachable target
  • Edge cases: start/target blocked, start equals target
  • Comparison with BFS when costs are uniform

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

Q2

After finding the shortest path cost, how would you reconstruct the actual path taken?

Algorithms & Data Structures
Author's notes

Blanked for a second.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Explain that path reconstruction is typically done by maintaining a predecessor (or parent) pointer for each node during the shortest path computation. After the algorithm completes, you can backtrack from the target node to the source using these pointers to obtain the path in reverse order, then reverse it to get the correct sequence.

Pro tip: Mention that for Dijkstra's algorithm, you can store the predecessor when you relax an edge, and for Bellman-Ford, you update the predecessor whenever you find a shorter path. Also, note that if you need to reconstruct paths frequently, you might store the entire path in each node, but that increases space complexity.

1. Identify the algorithm

State which shortest path algorithm you are using (e.g., Dijkstra, Bellman-Ford, BFS for unweighted graphs) because the reconstruction method is similar but the update condition differs.

2. Maintain predecessor pointers

During the algorithm, whenever you update the shortest distance to a node, also record the predecessor node that gave that distance (e.g., set parent[v] = u when relaxing edge u->v).

3. Backtrack from target

After the algorithm finishes, start at the target node and follow the predecessor pointers until you reach the source node, collecting nodes along the way.

4. Reverse the path

The collected nodes are in reverse order (from target to source), so reverse the list to get the path from source to target.

5. Handle edge cases

If the target is unreachable, the predecessor pointer may be null; ensure you check for that and return an empty path or appropriate indication.

Key Points to Mention

  • Predecessor array (or parent map) to store the previous node on the shortest path.
  • Update predecessor only when a shorter distance is found (relaxation condition).
  • Backtracking from destination to source using the predecessor array.
  • Reversing the path to get the correct order.
  • Time complexity: O(V) for backtracking, where V is the number of vertices in the path.
  • Space complexity: O(V) extra space for the predecessor array.

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