You are given a 2D grid where 0 represents an empty cell and 1 represents an obstacle. You want to move from the top-left corner (0, 0) to the bottom-right corner (m-1, n-1). You can move in four directions. The challenge is to find the minimum number of obstacles you need to remove to find a path to the destination.
This "Minimum Obstacle Removal to Reach Corner interview question" is a favorite at Google and TikTok because it's a perfect application of shortest-path algorithms on a graph. It tests if a candidate can model the grid as a graph where the weight of an edge is either 0 (empty cell) or 1 (obstacle cell).
The pattern is Dijkstra's Algorithm or 0-1 BFS. Since the weights are only 0 and 1, a 0-1 BFS using a Deque (double-ended queue) is more efficient than a standard Priority Queue. You push cells with weight 0 to the front of the deque and cells with weight 1 to the back, ensuring you always process the shortest path first.
Grid:
[[0, 1, 1], [1, 1, 0], [1, 1, 0]]
The most frequent mistake is using a standard BFS, which treats empty cells and obstacles the same. This only finds the path with the minimum number of steps, not the minimum number of obstacles. Another error is not using a "visited" array with distance tracking, which leads to redundant processing and potential infinite loops.
Whenever you encounter a grid problem where moves have different "costs," think Dijkstra. If the costs are specifically 0 and 1, reach for the 0-1 BFS with a Deque—it's a high-signal optimization that interviewers love to see.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Minimum Time to Visit a Cell In a Grid | Hard | Solve | |
| Minimum Cost to Make at Least One Valid Path in a Grid | Hard | Solve | |
| Find a Safe Walk Through a Grid | Medium | Solve | |
| The Maze II | Medium | Solve | |
| Find Minimum Time to Reach Last Room II | Medium | Solve |