← Amazon Interview Insights

Amazon·Software Engineer·Onsite - Coding / Algorithms·Intermediate

IntermediatePrefer not to say
Jun 2026

Summary

Amazon SWE coding round, one question the whole time: shortest path in a grid using BFS. Pretty standard stuff but I still managed to second-guess myself halfway through.

Questions Asked (1)

Q1

Given a 2D grid where cells are either walkable or blocked, find the minimum number of steps to reach a target cell from a start cell using 4-directional movement. Return -1 if no path exists.

Algorithms & Data Structures
Author's notes

I knew it was BFS immediately, which felt good, but then I wasted like two minutes debating whether to use a visited set or just mutate the grid in place.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use BFS to explore the grid level by level, tracking the number of steps from the start. BFS guarantees the shortest path in an unweighted grid, and if the target is never reached, return -1.

Pro tip: Mention that BFS is optimal here because each move costs 1, and discuss early termination when the target is found to save time. Also, clarify edge cases like start equals target or blocked start/target.

1. Clarify the problem

Confirm grid dimensions, movement directions (4-directional), and that start and target are valid cells. Ask about edge cases like start == target or blocked cells.

2. Choose BFS

Explain that BFS is ideal for finding the shortest path in an unweighted graph. Mention that DFS would not guarantee the shortest path.

3. Outline BFS algorithm

Initialize a queue with the start cell and a visited set. For each cell, explore its 4 neighbors, mark them visited, and enqueue if valid and unvisited. Track steps by level.

4. Handle termination and edge cases

Return the step count when the target is dequeued or enqueued. If the queue empties without reaching the target, return -1. Handle start == target by returning 0.

5. Analyze complexity

State that time complexity is O(R*C) since each cell is visited once, and space complexity is O(R*C) for the queue and visited set in the worst case.

Key Points to Mention

  • BFS guarantees shortest path in unweighted graphs
  • Use a queue for level-order traversal
  • Track visited cells to avoid cycles and redundant work
  • Check boundaries and blocked cells before enqueuing
  • Early exit when target is found
  • Time and space complexity: O(R*C)

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