Closest Bathroom / Desk on a Grid

Reported by candidates from Snowflake's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Snowflake OA. Under 2s to a working solution.
Founder's read

Snowflake reported this one in May 2026, and it looks friendly until you picture a big grid with a lot of desks. Running a search from every desk to find its nearest bathroom is the brute force trap. The clean answer is multi-source BFS: start from every bathroom at once and let the distances ripple outward. If you've got the OA coming up, learn this shape cold. And if your mind goes blank mid-assessment, StealthCoder is the invisible backup that reads the problem and hands you the approach.

The problem

You are given a two-dimensional office grid. Each cell contains one of three characters:
'B': a bathroom
'D': a desk
'_': an empty cell
Movement is allowed in the four cardinal directions, and the base version has no obstacles. For every desk cell, compute the shortest step distance to any bathroom.
Return an integer matrix with the same dimensions as grid. For cells containing desks, store the nearest-bathroom distance. For non-desk cells, store -1. If the grid has no bathrooms, every desk distance should be -1.

Function
closestBathroomDistances(grid: String[][]) → int[][]

Examples
Example 1
grid = [["B", "_", "D"], ["_", "D", "_"], ["D", "_", "B"]]
return = [[-1, -1, 2], [-1, 2, -1], [2, -1, -1]]
Each desk is two steps away from the nearest bathroom using 4-directional movement.
Example 2
grid = [["D", "_"], ["_", "D"]]
return = [[-1, -1], [-1, -1]]
There are no bathrooms in the grid, so no desk has a reachable bathroom.

Constraints
grid is a rectangular matrix.
Each cell is one of "B", "D", or "_".
Movement uses four directions: up, down, left, and right.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is flipping the direction. Instead of one BFS per desk, push every 'B' cell into the queue with distance 0, then expand in four directions. The first time BFS reaches a cell is its shortest distance, since all edges cost 1. Store distances in a matrix initialized to -1, and only keep the value for cells that are 'D'. Empty cells still need to be traversed, because they're walkable, but they output -1. The common pitfall is running BFS per desk, which blows up on large grids. Another is forgetting the no-bathroom case. If the queue starts empty, every answer stays -1 naturally, so you don't need a special branch. Mark cells visited when you enqueue, not when you pop, or you'll get duplicates. Total work is O(rows * cols). If you freeze on the OA, StealthCoder is the safety net that surfaces this multi-source setup in real time.

The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.

If this hits your live OA

You can drill Closest Bathroom / Desk on a Grid 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 for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.

Get StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as 01 matrix. If you have time before the OA, drill that.

⏵ The honest play

You've seen the question. Make sure you actually pass Snowflake's OA.

Snowflake reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Closest Bathroom / Desk on a Grid FAQ

What's the trick to the Snowflake closest bathroom problem?+

Multi-source BFS. Seed the queue with every bathroom at distance 0 and expand outward one layer at a time. The first visit to any cell is its shortest distance to a bathroom. One pass over the grid replaces a search per desk.

Why not run BFS from each desk?+

It repeats work. With many desks on a large grid, you'd traverse most of the grid again and again. Starting from all bathrooms together computes every desk's answer in a single O(rows * cols) sweep.

How do I handle a grid with no bathrooms?+

Initialize the result matrix to -1 and only write distances when BFS reaches a cell. With no bathrooms the queue starts empty, nothing gets updated, and every desk correctly stays -1. No extra branch needed.

Do empty cells count as walkable?+

Yes. The '_' cells are passable and the base version has no obstacles. BFS must travel through them to reach desks. They just output -1 in the final matrix, same as bathroom cells, since only desks store distances.

How should I prepare for this in 48 hours?+

Write multi-source BFS on a grid from scratch twice. Practice the queue seeding, the four-direction offsets, bounds checks, and marking visited on enqueue. Then test the two examples by hand, including the no-bathroom case. That covers the whole problem.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Snowflake.

OA at Snowflake?
Invisible during screen share
Get it