PracHub
QuestionsCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates a candidate's ability to implement and reason about grid-based shortest-path algorithms, covering graph traversal, state representation, and obstacle handling.

  • medium
  • Snowflake
  • Coding & Algorithms
  • Software Engineer

Find Shortest Grid Path

Company: Snowflake

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

You are given an `m x n` grid where `0` represents an open cell and `1` represents a blocked cell. You are also given a start cell `(sr, sc)` and a target cell `(tr, tc)`. From any open cell, you may move one step up, down, left, or right, as long as the next cell is inside the grid and not blocked. Return the minimum number of moves required to reach the target from the start. If the target is unreachable, return `-1`. Follow-up: Describe how you could also solve the problem using recursive depth-first search with backtracking and memoization, and explain the tradeoffs compared with a breadth-first search approach.

Quick Answer: This question evaluates a candidate's ability to implement and reason about grid-based shortest-path algorithms, covering graph traversal, state representation, and obstacle handling.

You are given an `m x n` grid where `0` represents an open cell and `1` represents a blocked cell. You are also given a start cell `(sr, sc)` and a target cell `(tr, tc)`. From any open cell, you may move one step up, down, left, or right, as long as the next cell is inside the grid and not blocked. Return the minimum number of moves required to reach the target from the start. If the target is unreachable, return `-1`. If the start equals the target (and is open), the answer is `0`. If either the start or the target cell is blocked, return `-1`. **Follow-up:** Describe how you could also solve the problem using recursive depth-first search with backtracking and memoization, and explain the tradeoffs compared with a breadth-first search approach. (BFS explores the grid level by level, so the first time it reaches the target is guaranteed to be along a shortest path; each cell is visited at most once, giving O(m*n) time. DFS with backtracking explores one path to its end before retreating, so to find the *shortest* path it must keep searching after the first hit and prune with the best-so-far distance; naive memoization is tricky because the shortest distance to a cell depends on the visited set along the current path, not just the cell coordinates, which can lead to exponential work or incorrect caching on grids with cycles.)

Constraints

  • 1 <= m, n <= 1000
  • grid[i][j] is either 0 (open) or 1 (blocked)
  • 0 <= sr, tr < m and 0 <= sc, tc < n
  • If the start or target cell is blocked, the answer is -1
  • Movement is only up, down, left, or right (no diagonals)

Examples

Input: ([[0,0,0],[0,1,0],[0,0,0]], 0, 0, 2, 2)

Expected Output: 4

Explanation: From the top-left to the bottom-right of a 3x3 grid with the center blocked; any shortest route around the obstacle takes 4 moves.

Input: ([[0,1],[1,0]], 0, 0, 1, 1)

Expected Output: -1

Explanation: The two open cells (0,0) and (1,1) are diagonal and separated by blocked cells; with only orthogonal moves the target is unreachable.

Input: ([[0]], 0, 0, 0, 0)

Expected Output: 0

Explanation: Start equals target on an open single cell, so zero moves are needed.

Input: ([[0,0,0,0],[1,1,1,0],[0,0,0,0]], 0, 0, 2, 0)

Expected Output: 8

Explanation: A wall on row 1 forces a detour to the right end of the grid and back down, costing 8 moves.

Input: ([[0,0],[0,0]], 0, 0, 1, 1)

Expected Output: 2

Explanation: An open 2x2 grid; the shortest path from a corner to the opposite corner is 2 moves.

Input: ([[0,1,0]], 0, 0, 0, 2)

Expected Output: -1

Explanation: A single-row corridor where the middle cell is blocked, cutting off the target.

Input: ([[1,0],[0,0]], 0, 0, 1, 1)

Expected Output: -1

Explanation: The start cell itself is blocked, so no path exists and the answer is -1.

Hints

  1. Because every move has the same cost (1), the shortest path is exactly the BFS distance from the start cell — a uniform-cost grid is the textbook case for breadth-first search rather than Dijkstra.
  2. Track a `visited` matrix so you never re-enqueue a cell; the first time BFS reaches the target is guaranteed to be along a shortest path.
  3. Handle the corner cases up front: start == target returns 0, and a blocked start or target returns -1.
  4. For the DFS follow-up, remember that the shortest distance to a cell depends on the path taken to get there (the current visited set), so a plain `dist[cell]` memo can be incorrect on grids with cycles — BFS sidesteps this entirely.
Last updated: Jun 26, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • AI Coding Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Implement a JSON Parser - Snowflake (hard)
  • Solve Matrix and Array Distance Problems - Snowflake (medium)
  • Solve Array Distance and Wiki Navigation - Snowflake (medium)
  • Implement Document Predicate APIs - Snowflake (medium)
  • Find Shortest Wiki Click Path - Snowflake (medium)