Shortest Path Around Rectangular Obstacles
Reported by candidates from DRW's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
DRW put this one in front of candidates in September 2026, and the first attempt usually dies on the same thing: marking blocked cells wrong. You get an N by M grid, K overlapping rectangles, and you need the fewest steps from (0, 0) to (N - 1, M - 1). It's a plain BFS on a grid once the obstacles are set up right. If the OA clock is running and your mind goes blank, StealthCoder is the invisible safety net that reads the problem and hands you a working solution.
The problem
You are given an N by M grid of cells. A cell has coordinates (x, y), where 0 <= x < N and 0 <= y < M. There are K axis-aligned rectangular obstacles. Rectangle i covers every cell whose coordinates satisfy X1[i] <= x <= X2[i] and Y1[i] <= y <= Y2[i]. Covered cells are blocked, and rectangles may overlap. Start at (0, 0). In one step, you may move to an unblocked cell directly above, below, left, or right of your current cell. Return the minimum number of steps needed to reach (N - 1, M - 1). Return -1 if no such path exists. In particular, return -1 when the start or destination cell is blocked. Function solution(N: int, M: int, X1: int[], Y1: int[], X2: int[], Y2: int[]) → int Examples Example 1 N = 6 M = 4 X1 = [2,1,4] Y1 = [0,1,3] X2 = [2,3,4] Y2 = [2,1,3] return = 10 One shortest route is (0,0) -> (0,1) -> (0,2) -> (1,2) -> (1,3) -> (2,3) -> (3,3) -> (3,2) -> (4,2) -> (5,2) -> (5,3). It uses 10 steps and avoids every blocked cell. Constraints N and M are positive integers. X1, Y1, X2, and Y2 have the same length K; K may be zero. For every i, 0 <= X1[i] <= X2[i] < N and 0 <= Y1[i] <= Y2[i] < M. Rectangle coordinates are zero-indexed and inclusive.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is BFS, because every step costs 1, so the first time you reach the target is the shortest path. The mistake that sinks a first attempt is the setup. Rectangles are inclusive on both ends and can overlap, so fill a boolean grid by looping x from X1 to X2 and y from Y1 to Y2. Overlap is harmless when you just set true again. Check the start and destination for blocking before you search and return -1 right away. Also watch the example: it ends at (5,3), so N is the x range and M is the y range. Don't swap them when you size the grid. Mark cells visited when you push them onto the queue, not when you pop, or the queue blows up. If you blank on the live OA, StealthCoder can supply the BFS skeleton while you handle the edge cases. Also handle K equal to zero, which just means an open grid.
If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.
You can drill Shortest Path Around Rectangular Obstacles cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as shortest path in binary matrix. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass DRW's OA.
DRW reuses patterns across OAs. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Shortest Path Around Rectangular Obstacles FAQ
What's the trick to the DRW shortest path around rectangles problem?+
Run BFS on the grid. Every move costs one step, so the first time you reach (N - 1, M - 1) is the minimum. Build a blocked grid from the rectangles first, then search with a queue and a visited array. Return -1 if the queue empties without reaching the target.
How do I handle overlapping rectangles?+
Don't worry about them. Loop over every cell each rectangle covers and set it to blocked. Setting an already blocked cell again changes nothing. You don't need to merge or dedupe rectangles, which keeps the setup simple and hard to get wrong.
What edge cases should I test before submitting?+
Test a blocked start, a blocked destination, K equal to zero, and a 1 by 1 grid where the answer is 0 if the cell is open. Also test a wall that fully splits the grid, which should return -1. Check that N is the x range and M is the y range.
Is the grid big enough that memory or time matters?+
BFS visits each cell at most once, so it runs in O(N times M) time and space. Filling rectangles costs up to the covered area per rectangle. The input doesn't state size limits, so keep it simple and use a flat visited array rather than anything fancy.
How do I prepare for this in 48 hours?+
Write grid BFS from scratch two or three times until the queue, visited marking, and four-direction loop feel automatic. Then add the obstacle-fill step. Practice returning distance by tracking levels or storing steps in the queue. That covers this problem and most of its variants.