Shortest Grid Path with Obstacle Elimination
Reported by candidates from Google's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The edge case that kills a naive solution on this Google OA, reported in September 2026, is treating a cell as visited once you've touched it. Reach the same cell with more eliminations left and it's a different state. This is shortest path with obstacle elimination, and it's breadth-first search over (row, col, eliminations used). Candidates who run plain BFS on the grid get wrong answers on cases like Example 1. If you've got the invite and 48 hours, learn this state trick cold. StealthCoder sits invisibly on your screen as a safety net if you blank mid-assessment.
The problem
You are given an m x n binary grid. A value of 0 is an open cell, and a value of 1 is an obstacle. Start at the top-left cell (0, 0). In one step, you may move one cell up, down, left, or right while remaining inside the grid. You may eliminate at most k obstacles by entering their cells. Return the minimum number of steps needed to reach the bottom-right cell (m - 1, n - 1). Return -1 when no valid path exists. Function shortestPathWithObstacleEliminations(grid: int[][], k: int) → int Examples Example 1 grid = [[0,0,0],[1,1,0],[0,0,0],[0,1,1],[0,0,0]] k = 1 return = 6 A shortest route uses six steps and eliminates one obstacle. Example 2 grid = [[0,1,1],[1,1,1],[1,0,0]] k = 1 return = -1 Every route to the destination enters more than one obstacle, so the elimination budget is insufficient. Example 3 grid = [[0]] k = 0 return = 0 The start is already the destination, so no step is needed. Constraints 1 <= m, n <= 40 grid[i][j] is 0 or 1. grid[0][0] = grid[m - 1][n - 1] = 0 0 <= k <= m * n
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is the state. Don't track visited cells, track visited (r, c, remaining k). BFS pops states in order of step count, so the first time you reach (m-1, n-1) is the minimum. Each move to a neighbor costs one step. If the neighbor is 1, you need remaining k greater than 0 and you decrement it. Pitfalls: marking only (r, c) as visited, which prunes better paths that arrive with more budget. Forgetting the 1x1 grid returns 0 before any loop. Another pitfall is exploring k up to m*n when it's pointless. If k >= m + n - 2, the answer is just m + n - 2, a Manhattan shortcut. Complexity is O(m*n*k) states, fine at 40x40. If you blank on the visited structure during the live OA, StealthCoder gives you the state layout and code in real time without the proctor seeing it.
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 Obstacle Elimination 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 Google's OA.
Google 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 Obstacle Elimination FAQ
What's the trick in the Google shortest grid path with obstacle elimination problem?+
Make the BFS state three-dimensional: row, column, and eliminations remaining. A cell reached with more budget isn't the same as one reached with less. Mark visited on that triple, and the first arrival at the bottom-right is your minimum step count.
How hard is this problem really?+
It's a hard-rated style problem, but only because of the state idea. The code is a standard BFS with a queue and a visited set. Once you see that k belongs in the state, it's about 25 lines. The constraints at 40x40 make it comfortable.
What edge cases should I test before submitting?+
Test a 1x1 grid with k=0, which returns 0. Test k large enough to ignore every obstacle. Test a case where no path exists, like Example 2, returning -1. Also test k=0 with obstacles blocking, and a grid where a longer route avoids obstacles entirely.
Can I speed up the search when k is large?+
Yes. If k >= m + n - 2, you can walk a monotone path ignoring every obstacle, so the answer is m + n - 2. Adding that early return avoids exploring the full state space and handles large k inputs instantly.
How do I prepare for this in 48 hours?+
Write the BFS from scratch twice. First solve plain grid shortest path, then add the budget dimension. Practice the visited structure as a 3D array or set of tuples. Run your own tests on the three examples, then time yourself to finish in under 20 minutes.