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.
Examples
01 · 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.
02 · 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
gridis a rectangular matrix.- Each cell is one of
"B","D", or"_". - Movement uses four directions: up, down, left, and right.
More Snowflake problems
- Effective Role PrivilegesPHONE SCREEN · Seen May 2026
- Minimum Clicks Between Wiki PagesOA · Seen May 2026
- Minimum Index Distance Between Person and CakeOA · Seen May 2026
- Simple Array Rotation GameSeen Apr 2026
- Max Element Indexes After RotationsOA · Seen Mar 2026
- String Formation (Also for AI/ML Software Engineer Intern :)OA · Seen Mar 2026
- Grid Traversal (Infrastructure Automation Internship)Seen May 2025
- Horizontal Pod Autoscaler (Infrastructure Automation Internship)Seen May 2025
public int[][] closestBathroomDistances(String[][] grid) {
// write your code here
}grid[["B", "_", "D"], ["_", "D", "_"], ["D", "_", "B"]]
expected[[-1, -1, 2], [-1, 2, -1], [2, -1, -1]]
sign in to submit