← Snowflake Interview Insights

Snowflake·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
May 2026

Summary

Snowflake software engineer interview with a grid-based BFS problem. Pretty standard coding round but the problem had enough nuance to trip you up if you weren't careful about multi-source traversal.

Questions Asked (1)

Q1

Given an m x n grid containing desk cells, bathroom cells, and empty cells, find for each desk the shortest path distance to its nearest bathroom using only up/down/left/right moves. Return a matrix with those distances, or -1 for desks that can't reach any bathroom.

Algorithms & Data Structures
Author's notes

My first instinct was to BFS from each desk individually and that works but it's slow if you have a lot of desks.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use multi-source BFS starting from all bathroom cells simultaneously to compute the shortest distance to the nearest bathroom for every cell. Then construct the result matrix by placing the computed distances for desk cells and -1 for unreachable desks.

Pro tip: Mention that multi-source BFS is optimal because it processes each cell once, achieving O(m*n) time, and discuss how to handle edge cases like no bathrooms or desks surrounded by walls.

1. Understand the problem and constraints

Clarify that the grid contains desks, bathrooms, and empty cells, and that movement is only up/down/left/right. Confirm that we need distances for desks only, with -1 for unreachable desks.

2. Choose the right algorithm

Select multi-source BFS because it efficiently computes shortest paths from multiple sources (bathrooms) in unweighted grids. Explain why BFS is preferred over running BFS from each desk.

3. Initialize and run multi-source BFS

Enqueue all bathroom cells with distance 0, then perform BFS level by level, updating distances for each visited cell. Use a distance matrix initialized to -1 or infinity.

4. Construct the result matrix

Iterate through the grid: for each desk cell, output its computed distance (or -1 if unreachable); for non-desk cells, output 0 or any placeholder as specified.

5. Analyze complexity and edge cases

State time and space complexity: O(m*n) time and O(m*n) space. Discuss edge cases: no bathrooms, no desks, desks already adjacent to bathrooms, and disconnected components.

Key Points to Mention

  • Multi-source BFS efficiently computes shortest distances from multiple sources in O(m*n) time.
  • Use a queue to process cells level by level, ensuring shortest paths.
  • Initialize distances to -1 (or infinity) and update when visiting cells.
  • Only desk cells need distances; other cells can be ignored in the output.
  • Handle unreachable desks by leaving distance as -1.
  • Space complexity can be optimized by using the grid itself for distances if allowed, but typically O(m*n) extra space is used.

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