← Snowflake Interview Insights

Snowflake·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
May 2026

Summary

Snowflake SWE coding round, just the one problem but it was a classic grid BFS that I kept overcomplicating in my head.

Questions Asked (1)

Q1

You have an m x n grid where cells are either walls (-1), gates (0), or empty rooms (INF). Fill every empty room with its shortest distance to any gate, in-place.

Algorithms & Data Structures
Author's notes

I knew BFS was the right call but I started thinking about running BFS from each empty room separately, which is obviously wrong and I caught it maybe two minutes in.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use a multi-source BFS starting from all gates simultaneously, updating distances level by level. This avoids redundant work and ensures each cell is visited once, giving O(mn) time. Alternatively, you could use DP with two passes, but BFS is more intuitive and directly models the problem.

Pro tip: Mention that BFS from gates is optimal because it processes cells in increasing distance order, and you can stop early if all reachable rooms are filled. Also, note that modifying the grid in-place is safe since gates are 0 and walls are -1, so you only update INF cells.

1. Clarify and Plan

Confirm that walls are -1, gates are 0, and empty rooms are INF. Decide on BFS from all gates as the approach, and explain why it's efficient.

2. Initialize Queue

Scan the grid to enqueue all gate coordinates. This sets up the multi-source BFS.

3. BFS Traversal

While the queue is not empty, pop a cell and explore its four neighbors. For each neighbor that is INF, set its distance to current distance + 1 and enqueue it.

4. Handle Unreachable Rooms

After BFS, any remaining INF cells are unreachable from any gate; leave them as INF. Mention this edge case.

5. Complexity Analysis

State that time complexity is O(mn) since each cell is enqueued at most once, and space is O(mn) for the queue in the worst case.

Key Points to Mention

  • Multi-source BFS from all gates simultaneously
  • In-place modification: only update INF cells
  • Time and space complexity: O(mn) time, O(mn) space
  • Handling unreachable rooms (remain INF)
  • Alternative DP approach with two passes (optional)
  • Edge cases: no gates, all walls, single cell

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