Shortest Delivery Route When You May Clear Up to K Obstacles

Last updated: November 29, 2026

Quick Overview

In a grid of open cells (0) and obstacles (1), find the fewest 4-directional steps from the top-left to the bottom-right corner when you may pass through at most k obstacles, or return -1.

Google
Coding & Algorithms
Software Engineer
Google
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Hard

0

0

0 solved


In a grid of open cells (0) and obstacles (1), find the fewest 4-directional steps from the top-left to the bottom-right corner when you may pass through at most k obstacles, or return -1.

Framed around robot or drone routing with a limited budget for clearing blockages, this tests state-space BFS, a pattern Google likes because it changes the constraint on a familiar grid problem. The interviewer wants the state to include the remaining budget and a sensible pruning rule.

What the Interviewer Expects
  • Explains why position alone is not a sufficient BFS state
  • Uses (row, col, removals_left) as the state
  • Prunes by keeping the best remaining budget seen per cell
  • Shortcuts when k is at least the Manhattan distance minus one
  • States O(R * C * k) bounds
Key Topics to Cover
BFS
State Space Search
Grids
Graphs
Pruning
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • Clearing an obstacle costs extra time instead of being budgeted; what algorithm now?
  • Return the path, not just its length.
  • Minimize obstacles cleared first, then path length.
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements
  • Input: R by C grid of 0/1 with start and end open, integer k at least 0.
  • Moving onto a 1 consumes one unit of budget.
  • Output: minimum steps, or -1....
High-Level Design

Plain BFS fails because two visits to the same cell can differ in remaining budget, and the one with more budget may be the only one that can finish. So BFS over states (r, c, rem). Each level is one ...


Submit Your Answer
Markdown supported