Shortest Distance from All Buildings
A hard-tier problem at 44% community acceptance, tagged with Array, Breadth-First Search, Matrix. Reported in interviews at Zenefits and 9 others.
You want to place a building on a grid where you can reach every other building with minimal total distance. Google, Meta, and DoorDash ask this hard matrix problem regularly. It sounds simple until you realize the greedy approach fails and you need to visit every cell and every building in the right order. The 44% acceptance rate reflects that most candidates either time out on a naive search or get the BFS logic backwards. This is the problem where knowing the exact pattern matters because re-deriving it live under pressure is brutal.
Companies that ask "Shortest Distance from All Buildings"
Shortest Distance from All Buildings is the kind of problem that decides whether you pass. StealthCoder reads the problem on screen and surfaces a working solution in under 2 seconds. Invisible to screen share. The proctor sees nothing. Built by an Amazon engineer who used it to pass JPMorgan's OA and system design loop.
Get StealthCoderThe trick is multi-source BFS, not single-source. You run BFS from every building simultaneously, layering outward and recording distances at each step. Most candidates try to compute distance from each cell to all buildings separately, which is slower and harder to code correctly. The real snag: you can't land on water or cells unreachable from some building. After BFS finishes, you scan for the cell with minimum total distance that every building could actually reach. StealthCoder matters here because the state-space is deceptive. The BFS loop is straightforward but the setup and validation pass trip up live solvers. If you blank on whether to use a single queue or per-building queues, or how to handle the reachability constraint, StealthCoder surfaces the working implementation in seconds, invisible during your screen share.
Pattern tags
You know the problem.
Make sure you actually pass it.
Shortest Distance from All Buildings recycles across companies for a reason. It's hard-tier, and most candidates blank under the timer. StealthCoder is the hedge: an AI overlay invisible during screen share. It reads the problem and surfaces a working solution in under 2 seconds. Built by an Amazon engineer who used it to pass JPMorgan's OA and system design loop. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Shortest Distance from All Buildings interview FAQ
Why can't I just run BFS once from the best cell?+
Because you don't know the best cell yet. You have to compute distance from every reachable cell to every building. Multi-source BFS does this in one pass by starting all buildings at distance 0 and expanding outward together. Single-source repeats the search, which is slower and harder to validate correctly.
Is this really asked at Meta and Google?+
Yes, it appears in their reported assessments. At hard difficulty and 44% acceptance, it's a real interview filter. Expect to see it if you're interviewing for backend or systems roles where spatial search and multi-point optimization come up.
What's the main pitfall when coding this live?+
Handling the reachability constraint. If a cell can't reach some building, it's invalid no matter the total distance. Most solvers forget to validate this during the final scan, or miscount buildings during BFS setup. The bug is quiet because the solution looks plausible.
Do I need to know the exact BFS queue order?+
Yes. All buildings start at distance 0 in the same queue. You process layer by layer, so when you visit a cell, you already have the shortest distance from at least one building. The order matters for correctness, not just speed.
How does this differ from a standard shortest-path problem?+
This is shortest sum of distances to multiple endpoints, not a path between two points. You need all-pairs distances, but computed efficiently. That's why multi-source BFS is the pattern. Array and Matrix topics matter because the grid constraint is part of the problem, not separate.
Want the actual problem statement? View "Shortest Distance from All Buildings" on LeetCode →