Shortest Distance from All Buildings
Reported by candidates from Waymo's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The data structure carrying this problem is a queue. Waymo's September 2026 OA reportedly asks for Shortest Distance from All Buildings, and the whole thing is BFS run once per building. You've got a grid with empty land, buildings and obstacles, and you need the empty cell with the smallest total walking distance to every building. If you blank on the bookkeeping mid-assessment, StealthCoder runs invisibly on your desktop and gives you a working solution as a safety net. Know the shape first, though. It's cleaner than it looks.
The problem
You are given a rectangular grid where 0 is empty land, 1 is a building, and 2 is an obstacle. Choose one empty cell on which to build a meeting point. Movement is allowed one cell up, down, left, or right through empty land. Buildings and obstacles cannot be crossed. Return the minimum possible sum of shortest-path distances from the chosen empty cell to every building. Return -1 when no empty cell can reach every building. Function shortestDistance(grid: int[][]) → int Examples Example 1 grid = [[1,0,2,0,1],[0,0,0,0,0],[0,0,1,0,0]] return = 7 Choosing row 1, column 2 gives distances 3, 3, and 1 to the three buildings, for total 7. Example 2 grid = [[1,0]] return = 1 The only empty cell is one step from the building. Example 3 grid = [[1]] return = -1 There is no empty cell on which to build the meeting point. Constraints 1 <= grid.length, grid[0].length <= 50. Every cell is 0, 1, or 2. The grid contains at least one building.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Start a BFS from each building, not from each empty cell. Buildings are usually fewer, and the grid is capped at 50 by 50. Keep two matrices: total distance summed across all BFS runs, and a count of how many buildings reached each cell. After all runs, only cells whose count equals the total building count are valid. Take the minimum distance among those, or return -1 if none qualify. The classic pitfall is forgetting the reach count, so a cell that sees only some buildings looks falsely cheap. Another is crossing buildings or obstacles during BFS. Treat both as walls. You can also prune early: if a BFS reaches fewer buildings than expected, return -1 immediately. Complexity is roughly buildings times rows times columns. If the queue logic slips on the live OA, StealthCoder is the hedge that keeps you moving.
If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.
You can drill Shortest Distance from All Buildings 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 would have shipped this the night before his JPMorgan OA if he'd had it.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as shortest distance from all buildings. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Waymo's OA.
Waymo reuses patterns across OAs. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Shortest Distance from All Buildings FAQ
What's the trick to Shortest Distance from All Buildings?+
Run BFS from every building and accumulate distances into a shared grid. Track how many buildings each empty cell was reached by. The answer is the minimum accumulated distance among cells reached by all buildings. Running BFS from every empty cell is the slower approach that tends to cause trouble.
How hard is this problem really?+
It's a hard-tagged problem, but the technique is plain BFS on a grid. The difficulty is bookkeeping: two matrices, a reach count, and correct wall handling. If you've written a grid BFS before, you can finish this in one sitting.
Why BFS from buildings instead of empty cells?+
Buildings are often fewer than empty cells, so fewer BFS runs. Distances are symmetric on an undirected grid, so the sum from building to cell equals cell to building. Either direction gives the same answer, but building-first is usually faster.
When do I return -1?+
Return -1 when no empty cell is reachable from every building. That covers a grid with no empty land, like [[1]], and cases where obstacles wall off a building. Check that the reach count equals the total building count before accepting any cell's distance.
How do I prepare for this in 48 hours?+
Write multi-source and single-source grid BFS from scratch twice. Practice a visited-per-run setup, either a fresh boolean grid or a marker trick. Then trace Example 1 by hand to confirm you get 7. Focus on the reach-count check, since that's where most wrong answers come from.