← Snapchat Interview Insights

Snapchat·Machine Learning Engineer·Technical Phone Screen·Intermediate

Intermediate
Jun 2026

Summary

Snapchat ML engineer interview with a coding round that was pretty much a BFS grid problem. Nothing wild, but it's the kind of question that feels easy until you're actually writing it under pressure.

Questions Asked (1)

Q1

Given an n by n binary matrix, find the length of the shortest path from the top-left cell to the bottom-right cell, where you can move in all 8 directions and only through cells with value 0. Return -1 if no path exists.

Algorithms & Data Structures
Author's notes

Classic BFS, and I knew it was BFS the moment I read it.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Model the matrix as an unweighted graph where each 0-cell is a node and edges connect to its 8 neighbors. Use BFS from the top-left cell to find the shortest path to the bottom-right cell, tracking distance and returning -1 if unreachable.

Pro tip: Mention that BFS is optimal for unweighted graphs and that using a deque with level-order traversal avoids storing distances separately. Also, note that early termination when reaching the target can save time.

1. Clarify and Validate Input

Confirm the matrix is n x n, binary, and that start and end cells are 0. Handle edge cases like n=1 or blocked start/end.

2. Choose BFS and Define State

Use BFS because all edges have equal weight. Each state is a cell (row, col) and distance from start.

3. Implement BFS with 8-Directional Moves

Initialize a queue with the start cell and a visited set. For each cell, explore all 8 neighbors that are within bounds, have value 0, and are unvisited.

4. Track Distance and Early Exit

Increment distance per BFS level. If the target cell is dequeued, return the distance. If the queue empties, return -1.

5. Analyze Complexity and Optimize

Time and space are O(n^2). Optionally, modify the matrix in-place to mark visited cells, saving space.

Key Points to Mention

  • BFS guarantees shortest path in unweighted graphs.
  • 8-directional movement includes diagonals, so check all 8 neighbors.
  • Use a queue (FIFO) and a visited set or in-place marking to avoid revisiting.
  • Handle edge cases: start or end blocked, n=1, no path.
  • Time and space complexity: O(n^2) for an n x n matrix.
  • Early termination when reaching the target can improve average performance.

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