← Snowflake Interview Insights

Snowflake·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
Jun 2026

Summary

Snowflake SWE interview with a grid traversal problem that sounds deceptively simple but has a clean optimal solution most people probably overthink.

Questions Asked (1)

Q1

Given a 2D grid containing desk cells, bathroom cells, open walkable space, and walls, find the shortest path distance from every desk to its nearest bathroom using 4-directional movement.

Algorithms & Data Structures
Author's notes

My first instinct was to BFS from each desk separately, which would have been a mess at scale.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Model the grid as a graph and run a multi-source BFS from all bathrooms simultaneously to compute the shortest distance to the nearest bathroom for every cell. Then, for each desk, read off the distance from the BFS result. This avoids running BFS from each desk individually, reducing time complexity.

Pro tip: Mention that multi-source BFS is optimal because it processes each cell once, and discuss how to handle unreachable desks (e.g., return -1 or infinity). Also, note that the grid can be large, so memory and time efficiency matter.

1. Clarify problem and constraints

Confirm the grid dimensions, movement directions (4-directional), and what to return for desks with no reachable bathroom. Ask about input size to choose the right algorithm.

2. Choose multi-source BFS

Initialize a queue with all bathroom cells and set their distance to 0. Use BFS to propagate distances to all reachable cells, updating distances as you go.

3. Implement BFS efficiently

Use a 2D array to store distances, initialized to infinity. Process cells level by level, skipping walls and already visited cells. Use a queue for O(1) enqueue/dequeue.

4. Extract results for desks

After BFS, iterate through the grid and collect distances for all desk cells. If a desk remains at infinity, it's unreachable; decide on a sentinel value (e.g., -1).

5. Analyze complexity and edge cases

State time complexity O(R*C) and space O(R*C). Discuss edge cases: no bathrooms, no desks, all walls, disconnected components.

Key Points to Mention

  • Multi-source BFS vs. running BFS from each desk (time complexity trade-off)
  • Time and space complexity: O(R*C) time, O(R*C) space
  • Handling unreachable desks (e.g., return -1 or infinity)
  • Using a queue for BFS and a distance matrix
  • 4-directional movement (up, down, left, right)
  • Edge cases: empty grid, no bathrooms, no desks, walls blocking paths

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