Shortest Path in a Grid with Obstacles Elimination
Reported by candidates from Mercor's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Mercor reportedly asked this one in September 2026, and the grid size is the first thing to read. A 40 x 40 board with up to k obstacle removals means blind path enumeration explodes fast, so brute-force DFS dies on the first big test. This is a BFS problem with one extra dimension. If you've seen shortest path on a plain grid, you're 80 percent there. The last 20 percent is realizing your state isn't just a cell. If you freeze during the live OA, StealthCoder sits invisibly on your screen as a safety net and hands you the state design.
The problem
You are given an m x n binary grid. A value of 0 is an empty cell and a value of 1 is an obstacle. Starting at the top-left cell, you may move one step up, down, left, or right. You may eliminate at most k obstacles. Return the minimum number of steps needed to reach the bottom-right cell, or -1 when it is impossible. Function shortestPath(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 Eliminating one obstacle allows a shortest route of six steps. Example 2 grid = [[0,1,1],[1,1,1],[1,0,0]] k = 1 return = -1 Every route to the destination requires eliminating more than one obstacle. Constraints 1 <= m, n <= 40 1 <= m * n grid[i][j] is either 0 or 1. grid[0][0] == 0 and grid[m - 1][n - 1] == 0. 0 <= k <= m * n
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick: run BFS where the state is (row, col, removals left). BFS gives the fewest steps because every move costs one. Track a visited set on the full triple, not just the cell. Reaching a cell with more removals left is strictly better, so a cell can be worth revisiting. Pitfall one: marking visited by cell only, which wrongly prunes better paths and returns -1 or a too-long answer. Pitfall two: forgetting that stepping onto a 1 costs one removal and you must have at least one left. Quick win: if k >= m + n - 2, the answer is m + n - 2 since you can walk straight through. State count is m * n * (k+1), fine for these limits. If your mind goes blank mid-assessment, StealthCoder is the hedge that surfaces this triple-state BFS so you can type it cleanly.
The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.
You can drill Shortest Path in a Grid with Obstacles 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. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.
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 Mercor's OA.
Mercor reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Shortest Path in a Grid with Obstacles Elimination FAQ
What's the trick for the Mercor obstacle elimination problem?+
Add the remaining removals to your BFS state. Each node is (row, col, k left). Moving into a 1 costs one removal, moving into a 0 costs none. The first time you pop the bottom-right cell, that step count is the answer. If the queue empties, return -1.
Why can't I just use a visited grid of cells?+
Because arriving at a cell with more removals left is better than arriving with fewer. If a cell-only visited set blocks the richer arrival, you can miss the real shortest route. Key visited on (row, col, remaining) or store the best remaining per cell.
How hard is this really?+
It's a LeetCode hard by label, but it's a standard BFS with one extra state dimension. If you can write grid BFS from memory, the only new step is the removals counter. Most failures come from the visited logic, not the algorithm.
What's the time complexity?+
O(m * n * k) states, each with four neighbor checks. With m and n up to 40, that's manageable. You can also cap k at m + n - 2 up front, since more removals than that never help, which shrinks the state space a lot.
How do I prepare in 48 hours?+
Write plain grid BFS twice from scratch, then add the removals dimension. Test the two given examples and a case with k = 0. Practice the early exit when k is large enough to walk a straight Manhattan path. Focus on visited-state correctness.