← Amazon Interview Insights

Amazon·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
Apr 2026

Summary

Amazon SWE interview with a grid pathfinding problem. Classic BFS setup but the twist with the one-time wall removal made it trickier than it looked at first glance.

Questions Asked (1)

Q1

Given an m x n binary grid where 1 means passable and 0 means blocked, find the minimum number of steps to travel from the top-left to the bottom-right cell. You can convert at most one blocked cell into a passable cell during your journey. Return -1 if it's impossible even with that conversion.

Algorithms & Data Structures
Author's notes

My first instinct was plain BFS and I actually started coding it before remembering the conversion mechanic.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use 0-1 BFS on a state graph where each state is (row, col, used_conversion). Treat moving to a passable cell as cost 0 and moving to a blocked cell as cost 1, but only if the conversion hasn't been used. The answer is the minimum cost to reach the bottom-right cell with either 0 or 1 conversions used.

Pro tip: Emphasize that 0-1 BFS with a deque is optimal for this problem because edge weights are only 0 or 1, giving O(mn) time, which is better than Dijkstra's O(mn log(mn)). Also, clarify that the conversion can be used at most once, and the state space naturally enforces that.

1. Clarify the problem and constraints

Confirm that you can convert at most one blocked cell (0 to 1) during the journey, and that you need the minimum number of steps (moves) from (0,0) to (m-1,n-1). Ask about edge cases like start or end being blocked.

2. Define the state space

Model each state as (row, col, used) where used is 0 or 1 indicating whether the conversion has been used. This captures all necessary information for the shortest path.

3. Choose the algorithm

Use 0-1 BFS with a deque: moving to a passable cell has cost 0 (push front), moving to a blocked cell has cost 1 (push back) only if used=0. This efficiently computes the shortest path in O(mn) time.

4. Handle edge cases and return

If the start or end is blocked and cannot be converted (e.g., start is blocked and you have no conversion left), return -1. Otherwise, return the minimum distance to (m-1,n-1,0) or (m-1,n-1,1).

5. Analyze complexity and test

State that time and space are O(mn). Walk through a small example to verify correctness, including cases where conversion is needed and where it's impossible.

Key Points to Mention

  • 0-1 BFS with deque for O(mn) time complexity
  • State representation: (row, col, used_conversion)
  • Edge weights: 0 for passable cell, 1 for blocked cell (if conversion available)
  • Handling of start and end cells being blocked
  • Comparison with Dijkstra's algorithm and why 0-1 BFS is more efficient
  • Space complexity O(mn) for distance array and deque

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