Solve grid shortest path with BFS
DisneySoftware Engineer · OnsiteLast updated
This question evaluates understanding of grid traversal and shortest-path graph search, including modeling a 2D grid with obstacles and reasoning about boundary and edge cases, within the Coding & Algorithms domain.
Given a 2D grid containing 0s (open cells) and 1s (walls), and coordinates for a start (sr, sc) and a target (tr, tc), return the length of the shortest path from start to target using 4-directional moves (up, down, left, right). If no path exists, return -1. Specify the algorithm, analyze time and space complexity, and discuss edge cases such as starting equals target, boundaries, and large grids.
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.