Walls and Gates
A medium-tier problem at 63% community acceptance, tagged with Array, Breadth-First Search, Matrix. Reported in interviews at DoorDash and 7 others.
Walls and Gates hits your assessment screen, and you've got a matrix full of empty rooms, walls, and gates. DoorDash, Google, Uber, Shopify, and Salesforce ask this one regularly. The trap: candidates overthink distance calculation or try greedy approaches that collapse on corner cases. The fix is pure BFS from all gates simultaneously, but the setup matters. If you blank on the multi-source BFS pattern during your live OA, StealthCoder surfaces the working solution in seconds, invisible to the proctor.
Companies that ask "Walls and Gates"
Walls and Gates 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 engineer who got tired of watching his cohort grind for six months and still get filtered at the OA stage.
Get StealthCoderThe problem requires you to calculate the shortest distance from every empty room to the nearest gate. Most candidates try BFS from each room individually, which tanks on larger matrices. The real pattern is reverse BFS: start from all gates at once, expanding outward simultaneously. This is a classic multi-source shortest-path problem dressed as a matrix puzzle. You mark distances as you explore, so each cell gets visited exactly once. Common pitfalls include forgetting to initialize the queue with all gates, not tracking visited cells properly, or confusing the direction of your search. The Array and Matrix operations are straightforward, but the BFS ordering is where people lose points. StealthCoder handles the queue logic and distance tracking for you if the pattern doesn't click during the timed assessment.
Pattern tags
You know the problem.
Make sure you actually pass it.
Walls and Gates recycles across companies for a reason. It's medium-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 engineer who got tired of watching his cohort grind for six months and still get filtered at the OA stage. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Walls and Gates interview FAQ
Is this problem actually asked at top companies?+
Yes. It appears in live assessments at Google, Uber, DoorDash, Shopify, and Salesforce. The 63% acceptance rate means it's Medium difficulty but not a gimme. Companies use it to test whether you can recognize multi-source shortest-path problems, not just single-source BFS.
What's the trap most candidates hit?+
Running BFS from each empty room separately. That's O(n * m * (n + m)) and timeouts. The trick is starting BFS from all gates simultaneously in one pass. If you haven't drilled multi-source BFS before, this pattern feels backwards until it clicks.
How does this relate to the BFS topic?+
It's the advanced version of standard BFS. Instead of exploring from a single source, you initialize the queue with multiple sources and explore outward together. Same algorithm, different setup. If you can solve this, single-source BFS is trivial by comparison.
Will I have time to code this during an assessment?+
Yes, if you know the pattern. The implementation is straightforward once you understand the multi-source approach: queue all gates, pop and expand, update distances. If you're unsure on the actual OA, that's exactly when StealthCoder runs invisibly and hands you a clean solution.
What Matrix and Array skills does this actually test?+
2D indexing, direction vectors for neighbors, and in-place updates. Nothing exotic. The real test is algorithmic pattern recognition. You need to see 'find nearest X for all cells' and immediately think 'multi-source BFS', not simulation or greedy.
Want the actual problem statement? View "Walls and Gates" on LeetCode →