← Snowflake Interview Insights

Snowflake·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
Apr 2026

Summary

Snowflake software engineer interview with a grid-based coding problem. Pretty standard algorithmic round, nothing too flashy, but the problem had a few wrinkles worth thinking through.

Questions Asked (1)

Q1

Given a 2D grid where some cells represent cakes and others represent people, find the minimum Manhattan distance between any person and any cake.

Algorithms & Data Structures
Author's notes

My first instinct was to just brute-force every person-cake pair and compute the distance.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Clarify the problem constraints (grid size, number of people/cakes) and then propose a multi-source BFS from all cakes simultaneously to compute the shortest Manhattan distance to any person. Alternatively, discuss a two-pass dynamic programming approach that computes distances in O(mn) time, which is optimal for large grids.

Pro tip: Mention that multi-source BFS is equivalent to computing the Manhattan distance transform, and that you can optimize memory by using a 2D array of integers or even a single array if the grid is stored row-major. Also, note that if the grid is extremely large and sparse, a k-d tree or sorting-based approach might be more efficient.

1. Clarify constraints and edge cases

Ask about grid dimensions, number of people and cakes, whether movement is allowed only in 4 directions, and if there are no people or no cakes. This ensures you design the right algorithm.

2. Choose the right algorithm

For dense grids, use multi-source BFS from all cakes to compute distances to all cells, then find the minimum among people. For large grids, consider the two-pass dynamic programming (distance transform) approach which runs in O(mn) time and O(mn) space.

3. Implement and optimize

Code the BFS with a queue, initializing it with all cake positions. Alternatively, implement the two-pass DP: first pass from top-left to bottom-right, second pass from bottom-right to top-left, updating distances. Use early termination if a person is found at distance 0.

4. Analyze complexity and trade-offs

Explain that BFS is O(mn) time and space, and the DP approach is also O(mn) but with lower constant factors. Discuss when to use each (e.g., BFS for sparse cakes, DP for dense).

5. Test with examples

Walk through a small example to verify correctness, such as a 3x3 grid with one cake and one person. Also test edge cases like no cakes or no people.

Key Points to Mention

  • Manhattan distance definition: |x1 - x2| + |y1 - y2|
  • Multi-source BFS: initialize queue with all cakes, then BFS layer by layer
  • Two-pass dynamic programming (distance transform) for O(mn) time
  • Time and space complexity: O(mn) for both approaches
  • Edge cases: no cakes, no people, multiple cakes/people
  • Optimization: early termination when distance 0 is found, or using 1D array for DP

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