Shortest Grid Path With One Wall Break
Reported by candidates from Amazon's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The mistake that sinks most first attempts at this Amazon problem is running a plain BFS and treating the wall break as a bolt-on afterthought. This one was reported in September 2026, and it's a shortest path on a grid where you can enter at most one wall cell. It's a BFS with extra state. If you've seen the classic obstacle elimination problem, you're ahead. If you blank when the OA timer starts, StealthCoder runs invisibly on your screen as a safety net and gives you the approach in real time.
The problem
Given a rectangular binary matrix grid, a cell containing 1 is open and a cell containing 0 is a wall. Start at the top-left cell and move to the bottom-right cell. Each move goes one cell up, down, left, or right. During the route, you may break and enter at most one wall. Return the minimum number of moves needed to reach the destination, or -1 when no valid route exists. Function minimumMovesWithOneWallBreak(grid: int[][]) → int Examples Example 1 grid = [[1,0,1],[0,0,1],[1,1,1]] return = 4 Break the wall at (0,1), then move through (0,2) and (1,2) to reach (2,2) in four moves. Example 2 grid = [[1,0,1],[0,0,0],[1,0,1]] return = -1 Every route from the start to the destination would enter at least two wall cells, so one wall break is insufficient. Example 3 grid = [[1]] return = 0 The start is already the destination, so no move is needed. Constraints 1 <= grid.length <= 200 1 <= grid[i].length <= 200 Every row has the same length. grid[i][j] is either 0 or 1. The top-left and bottom-right cells contain 1.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is that a cell isn't one state, it's two or more. Your state is (row, col, wallsUsed), where wallsUsed is 0 or 1. Run BFS from (0,0,0). Moving into an open cell keeps wallsUsed the same. Moving into a wall cell is allowed only if wallsUsed is 0, and it flips to 1. The first time you pop (m-1, n-1, any), return the distance. The common pitfall is a visited array indexed only by row and column. That blocks a later path that arrives at the same cell with the break still unused, and it gives wrong answers. Use a visited array of size m by n by 2. Handle the 1x1 grid by returning 0 before the loop. With a 200 by 200 grid, that's about 80,000 states, so BFS is easily fast enough. If the assessment rattles you, StealthCoder is the hedge for the live OA.
StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.
You can drill Shortest Grid Path With One Wall Break cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. If you're reading this with an OA window open, you're who this was built for.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as shortest path in a grid with obstacles elimination. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Amazon's OA.
Amazon reuses patterns across OAs. If you're reading this with an OA window open, you're who this was built for. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Shortest Grid Path With One Wall Break FAQ
What's the trick in the Amazon shortest grid path with one wall break?+
Track the wall break inside the BFS state. Each state is (row, col, breaksUsed) with breaksUsed at 0 or 1. Open cells keep the count, wall cells need breaksUsed 0 and flip it to 1. The first time you reach the bottom-right, that distance is the answer.
Why does plain BFS with a 2D visited array fail here?+
A cell reached with the break already spent is worse than the same cell reached with the break unused. A 2D visited array lets the worse arrival block the better one. You'd return -1 or a too-long path on valid grids. Keep visited per breaks-used layer.
What's the time and space complexity?+
Each of the m times n cells has two states, and each state checks four neighbors. That's O(m * n) time and O(m * n) space for the visited array and queue. With a 200 by 200 grid at most, that's comfortably small.
What edge cases should I test before submitting?+
Test the 1x1 grid, which returns 0 with no moves. Test a grid where one break is enough, like example 1. Test one where two breaks would be needed, like example 2, which returns -1. Also test a fully open grid, where the break is never used.
How do I prepare for this in 48 hours?+
Write the BFS with a state tuple from scratch twice. Use a queue of (r, c, k) and a 3D visited array. Then change it to allow k breaks. This is a common Amazon-style grid pattern, and the state-extension idea carries to similar problems.