Compute shortest path in a 2D grid
SnapchatSoftware Engineer · OnsiteLast updated
This question evaluates understanding of graph traversal and pathfinding concepts, including modeling a 2D grid as a graph and reasoning about shortest-path computations and obstacle handling.
Given an m x n grid where:
0represents an empty cell you can move through1represents a blocked cell you cannot enter- movement is allowed in 4 directions (up, down, left, right)
You are also given coordinates start = (sr, sc) and target = (tr, tc) (both are inside the grid).
Return the length of the shortest path (number of moves) from start to target. If target is unreachable, return -1.
Explain the algorithm you would use and its time/space complexity.
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.