Closest Bathroom / Desk on a Grid
Updated
Problem statement
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]]Moved or new
Each desk is two steps away from the nearest bathroom using 4-directional movement.
Example 2
grid = [["D", "_"], ["_", "D"]]return = [[-1, -1], [-1, -1]]Moved or new
There are no bathrooms in the grid, so no desk has a reachable bathroom.
Constraints
gridis a rectangular matrix.- Each cell is one of
"B","D", or"_". - Movement uses four directions: up, down, left, and right.
More Snowflake Phone Screen questions
- Connect Four from the Last MovePHONE SCREEN · Seen Nov 2026
- Minimum Edits to a Perfect Binary TreePHONE SCREEN · Seen Nov 2026
- Configurable Per-Client Token BucketsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Find Valid SET Card TriplesOA · PHONE SCREEN · Seen Nov 2026
- Minimum Index Distance Between Person and CakeOA · PHONE SCREEN · Seen Nov 2026
- Minimum N-ary Tree Depth DeletionsOA · PHONE SCREEN · Seen Nov 2026
- Sum of Binary Tree Boundary NodesOA · PHONE SCREEN · Seen Nov 2026
- Greedy Person-Cake Matching DistancePHONE SCREEN · Seen Nov 2026
- Tree Height After Child PromotionPHONE SCREEN · Seen Oct 2026
- Minimum StackPHONE SCREEN · Seen Oct 2026