← Snowflake Interview Insights

Snowflake·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
Apr 2026

Summary

Snowflake SWE coding round, one problem the whole session. Grid BFS thing that looks approachable until you realize they want optimal complexity and a clean multi-source setup, not just a per-desk Dijkstra loop.

Questions Asked (1)

Q1

Given a 2D character grid containing desks, bathrooms, empty spaces, and walls, find the shortest walking distance from each desk to its nearest bathroom using only 4-directional moves. Return a map of desk coordinates to minimum distances, or -1 if a desk has no path to any bathroom.

Algorithms & Data Structures
Author's notes

My first instinct was to BFS from each desk separately and I even started coding it up.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

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.

1. Clarify grid semantics and constraints

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.

2. Initialize multi-source BFS

Create a distance matrix initialized to -1 (unvisited). Enqueue all bathroom cells with distance 0, as they are the sources.

3. Run BFS level by level

Process the queue, exploring 4-directional neighbors. For each unvisited non-wall neighbor, set its distance to current distance + 1 and enqueue it.

4. Collect results for desks

After BFS, iterate through the grid. For each desk cell, record its distance from the matrix; if still -1, mark as unreachable.

5. Analyze complexity and edge cases

State that time and space are O(R*C). Discuss edge cases: no bathrooms, no desks, desks blocked by walls, and multiple bathrooms.

Key Points to Mention

  • Multi-source BFS treats all bathrooms as sources at distance 0, ensuring each cell gets the minimum distance to any bathroom.
  • 4-directional movement means only up, down, left, right neighbors are considered.
  • Walls are impassable and should be skipped during BFS.
  • Desks and bathrooms are passable; empty spaces are also passable.
  • If a desk is never reached, its distance remains -1, indicating no path.
  • Time and space complexity are O(R*C) where R and C are grid dimensions.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.