Skip to interview prep
Apexed

62. Shortest Path in a Grid (BFS)

MediumGraphs

grid contains 0 (open) and 1 (wall). Moving up, down, left or right, return the fewest steps from the top-left to the bottom-right cell, or -1 if it cannot be reached.

Example 1

Input: grid = [[0, 0, 0], [1, 1, 0], [0, 0, 0]]
Output: 4

Constraints

  • 1 ≤ rows, columns ≤ 100

Tab indents · Ctrl/⌘ + Enter runs · Esc then Tab leaves the editor

Run checks these cases; the expected output comes from the reference solution. Submit judges all 4 hidden and sample tests.

Code runs only in your browser in an isolated sandbox. Progress and submissions are saved on this device.

Shortest Path in a Grid (BFS) — coding interview practice — Apexed