Shortest Robot Path on an Occupancy Grid With Limited Obstacle Removals
Last updated: November 29, 2026
Quick Overview
Given a 0/1 occupancy grid where 1 is an obstacle, find the fewest 4-directional moves from the top-left to the bottom-right cell if the robot may pass through at most k obstacle cells, or return -1.
MathWorks
November 29, 20260
0
0 solved
Given a 0/1 occupancy grid where 1 is an obstacle, find the fewest 4-directional moves from the top-left to the bottom-right cell if the robot may pass through at most k obstacle cells, or return -1.
MathWorks ships Robotics System Toolbox and Navigation Toolbox with occupancy maps and planners, so grid search questions appear in its loop. The interviewer checks that you extend BFS with a resource dimension, prune states correctly, and reason about the state space size.
What the Interviewer Expects
- Recognizes plain BFS is insufficient because the remaining budget is part of the state
- Uses BFS over (row, col, removals_left) since every move costs 1
- Prunes a state when the cell was already reached with at least as many removals left
- Caps k at the Manhattan path length to bound work
- States O(R * C * k) time and space
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- How would you change the solution if passing through an obstacle cost 5 moves instead of 1?
- How would you return the actual path, and the obstacle cells it removes?
- Navigation Toolbox planners like A* use heuristics. What heuristic is admissible here and does it still help with the budget dimension?
- How would you handle 8-directional movement with diagonal cost sqrt(2)?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Input: R x C grid of 0/1, start (0,0) and goal (R-1,C-1), integer k >= 0. Moving into an obstacle consumes one removal. Output: minimum number of moves, 0 if start is the goal, -1 if unreach...
High-Level Design
Every move costs the same, so BFS by layers finds shortest paths, but the state must be (r, c, left). Rather than a 3D visited array, store best_left[r][c]: the most removals left with which this ...