← Snowflake Interview Insights
My first instinct was to BFS from each desk separately and I even started coding it up.
Use multi-source BFS starting from all bathrooms simultaneously to compute the shortest distance to the nearest bathroom for every cell. Then iterate over the grid and collect distances for desk cells, returning -1 for any desk that remains unreachable.
Pro tip: Mention that multi-source BFS is optimal because it avoids redundant searches from each desk, and clarify that walls are impassable while bathrooms and desks are passable. Also note that if the grid is very large, you can stop early once all desks are reached.
Confirm the characters representing desks, bathrooms, empty spaces, and walls, and whether desks/bathrooms are passable. Ask about grid size to discuss time/space complexity.
Create a distance matrix initialized to -1 (unvisited). Enqueue all bathroom cells with distance 0, as they are the sources.
Process the queue, exploring 4-directional neighbors. For each unvisited non-wall neighbor, set its distance to current distance + 1 and enqueue it.
After BFS, iterate through the grid. For each desk cell, record its distance from the matrix; if still -1, mark as unreachable.
State that time and space are O(R*C). Discuss edge cases: no bathrooms, no desks, desks blocked by walls, and multiple bathrooms.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.