Shortest Warehouse Robot Path When It Can Clear K Obstacles

Last updated: November 29, 2026

Quick Overview

A warehouse floor is a grid of 0 (open) and 1 (pallet obstacle). A robot moves up, down, left or right from the top-left cell to the bottom-right cell, and can clear at most k obstacles along the way. Return the minimum number of moves, or -1 if impossible.

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

0

0

0 solved


A warehouse floor is a grid of 0 (open) and 1 (pallet obstacle). A robot moves up, down, left or right from the top-left cell to the bottom-right cell, and can clear at most k obstacles along the way. Return the minimum number of moves, or -1 if impossible.

An Amazon Robotics flavored state-space search question for onsite rounds. The interviewer checks that you extend BFS state to include remaining clears, prune with the best remaining-k per cell, and spot the Manhattan-distance shortcut when k is large.

What the Interviewer Expects
  • Model state as (row, col, clears_left)
  • Use BFS since every move costs 1
  • Prune states dominated by a visit with more clears left
  • Short-circuit when k >= rows + cols - 2
  • Explain why plain visited-by-cell is wrong
Key Topics to Cover
BFS
State Space Search
Grids
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
  • What if clearing an obstacle costs extra time instead of one move?
  • How would you plan for many robots without collisions?
  • How would you return the path itself?
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

Grid is non-empty, start and end cells are open. 0 <= k. Return an int number of moves....

High-Level Design

Plain BFS with a visited set per cell fails, because reaching a cell with more clears left is strictly better than reaching it earlier with fewer. So the state is (r, c, remaining). BFS by levels stil...


Submit Your Answer
Markdown supported