← Amazon Interview Insights

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

Intermediate
May 2026

Summary

Amazon SWE coding round, basically just a grid BFS problem. Pretty standard stuff but the pressure of getting the edge cases right in real time is a different beast.

Questions Asked (1)

Q1

Given a 2D binary grid where 1 is walkable and 0 is blocked, find the minimum number of steps to travel from the top-left corner to the bottom-right corner using 4-directional movement. Return -1 if no path exists.

Algorithms & Data Structures
Author's notes

Classic BFS shortest path on a grid.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Recognize this as a shortest path problem on an unweighted grid, so BFS is the optimal algorithm. Start BFS from the top-left cell, exploring all 4 directions level by level, and return the distance when reaching the bottom-right. Handle edge cases like blocked start/end or no path by returning -1.

Pro tip: Mention that BFS guarantees the shortest path in unweighted graphs, and proactively discuss space/time complexity (O(m*n)) and potential optimizations like bidirectional BFS or A* if the grid is huge.

1. Clarify and Validate Input

Confirm grid dimensions, movement rules, and edge cases (e.g., start or end blocked, empty grid). Check if the start or end is 0 and immediately return -1.

2. Choose BFS and Initialize

Use a queue for BFS, starting with the top-left cell (0,0) and distance 0. Mark visited cells to avoid cycles, either by modifying the grid or using a separate visited set.

3. Explore Neighbors Level by Level

For each cell, check its 4-directional neighbors. If a neighbor is within bounds, walkable (1), and unvisited, add it to the queue with distance+1 and mark visited.

4. Terminate and Return Result

If the bottom-right cell is reached, return its distance. If the queue empties without reaching it, return -1.

5. Analyze Complexity and Optimizations

State time and space complexity O(m*n). Optionally discuss bidirectional BFS or A* for large grids, and note that DFS would not guarantee shortest path.

Key Points to Mention

  • BFS is optimal for unweighted shortest path problems.
  • Use a queue and track distance (either in queue or separate level counter).
  • Mark visited cells to avoid infinite loops; can modify grid in-place to save space.
  • Handle edge cases: start/end blocked, no path, 1x1 grid.
  • Time and space complexity: O(m*n) where m and n are grid dimensions.
  • Alternative algorithms: bidirectional BFS, A* with Manhattan heuristic (if allowed).

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