Find Shortest Grid Path
SnowflakeSoftware Engineer · Technical ScreenLast updated
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.
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.
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.