← Questions

Shortest path between two cells in a grid

0
SnowflakeCodingLast asked Feb 2026

A coding interview question asked in Snowflake's two coding screens round, last asked Feb 2026. It tests bfs, dfs and grids.

You get an m x n grid where 0 is an open cell and 1 is blocked, plus a start cell and a target cell. From an open cell you may step up, down, left or right onto an open cell inside the grid. Return the fewest steps from start to target, or -1 if the target cannot be reached.

Interface

Unless you agree on a different interface with the interviewer, assume:

  • shortest_path(grid: list[list[int]], sr: int, sc: int, tr: int, tc: int) -> int returns the minimum number of moves, 0 when start equals target, and -1 when either cell is blocked or no path exists.

Example

grid = [[0,0,0],
        [0,1,0],
        [0,0,0]]
shortest_path(grid, 0, 0, 2, 2)  ->  4     # around the blocked centre

shortest_path([[0,1],[1,0]], 0, 0, 1, 1)  ->  -1    # only diagonal contact

grid = [[0,0,0,0],
        [1,1,1,0],
        [0,0,0,0]]
shortest_path(grid, 0, 0, 2, 0)  ->  8     # along the top, down the right column, back along the bottom

Constraints

  • 1 <= m, n <= 1000.
  • Moves are orthogonal only; every move costs 1.

Clarifying questions

  • Is every move the same cost, or can cells have weights?
  • Should a blocked start or target return -1?
  • Do we need the path itself or only its length?
  • How big can the grid get (recursion depth matters for DFS)?

What a strong answer covers

  • Breadth-first search from the start with a visited matrix, marking cells when they are enqueued; the first time the target is dequeued its distance is minimal because BFS expands in order of distance.
  • O(m * n) time and space, and the edge cases: start equals target, blocked start or target, a 1x1 grid.
  • The follow-up: a recursive DFS with backtracking explores whole paths and has to keep searching after the first hit, pruning with the best length so far. A plain memo of "shortest distance from this cell" is wrong on grids with cycles, because the answer depends on which cells the current path has already used.
  • When DFS with memoization is correct: when moves only go one way (for example only right and down), the grid is a DAG and dist[cell] can be cached.
  • Tests for unreachable targets and large open grids.

Follow-ups

  • Rewrite it as DFS with backtracking and memoization, and compare it with BFS on correctness and cost.
  • Only right and down moves are allowed: how does the DP look now?
  • Cells have different entry costs.
  • You may remove up to k obstacles along the way.

Where it's asked

  • Snowflake · Two coding screens · Last asked Feb 202660 min · Two back-to-back live coding rounds, often on the same day; some loops pair coding with system design · Varies: some screens forbid AI, some 2026 screens allow it. The scheduling email says.

What it tests

bfs · dfs · grids · memoization

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding