Shortest Courier Path Through a Grid With K Closures Allowed
Last updated: November 29, 2026
Quick Overview
A courier moves on a grid where 1 marks a closed street block; the courier may pass through at most k closed blocks (with permission). Return the minimum number of steps from top-left to bottom-right, or -1.
Uber
November 29, 20260
0
0 solved
A courier moves on a grid where 1 marks a closed street block; the courier may pass through at most k closed blocks (with permission). Return the minimum number of steps from top-left to bottom-right, or -1.
Uber Eats couriers on bikes can sometimes cut through pedestrian plazas or closed streets at a cost, so routing with a limited budget of exceptions is realistic. The onsite tests whether you expand the BFS state to include remaining budget and bound the state space correctly.
What the Interviewer Expects
- Expand BFS state to (row, col, closures used)
- Explain why plain visited-by-cell is wrong
- Bound the state space at R x C x (k + 1)
- Use the Manhattan distance shortcut when k is large
- Return -1 when unreachable
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 prune states that are dominated (same cell, more closures used, not shorter)?
- What if passing a closed block costs 5 extra steps instead of being budgeted?
- How would A* with a Manhattan heuristic help here?
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: grid of 0/1, integer
k >= 0. Start(0,0)and goal(R-1,C-1)are open. - Each move is 4-directional and costs 1 step. Entering a 1 uses one unit of
k. - Output: minimum steps or -1....
High-Level Design
Plain BFS with a per-cell visited set fails: reaching a cell early by spending closures can block a slightly longer path that saves closures for later. So the state is (r, c, used). BFS on this laye...