FastPrepClosest Bathroom / Desk on a Grid
Problems/Closest Bathroom / Desk on a Grid

Closest Bathroom / Desk on a Grid

Snowflake logoSnowflakeFULLTIMEPHONE SCREEN

Updated Jul 2026

QuestionFunctionExamplesConstraints

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]]
grid
0120B_D1_D_2D_B
→
return
0120-1-121-12-122-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]]
grid
010D_1_D
→
return
010-1-11-1-1
Moved or new

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.

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

Snowflake interview experiences

  • Snowflake AI Coding Interview, Experienced Hires: 3 Experiences
All Snowflake interview questionsSee Snowflake hiring insights
Saved
class Solution {
    public int[][] closestBathroomDistances(String[][] grid) {
      // write your code here
    }
}
Checking account…