Shortest Path in a Binary Matrix
Reported by candidates from Meta's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The detail that matters in this Meta problem is the eight-direction move set, and it's what trips people up. Meta reported this one in September 2026: shortest path in an n x n binary matrix, top-left to bottom-right, only through 0 cells, diagonals allowed. It's breadth-first search on an implicit grid graph. If you've seen it, it's 15 minutes of work. If you blank on the setup, StealthCoder is the safety net running invisibly during the live OA. Either way, you only need the pattern and the three edge cases below.
The problem
Given an n x n binary matrix, return the number of cells in the shortest clear path from the top-left cell to the bottom-right cell. A clear path visits only cells containing 0 and may move horizontally, vertically, or diagonally to any of the eight neighboring cells. Return -1 when no clear path exists. Function shortestPathBinaryMatrix(grid: int[][]) → int Examples Example 1 grid = [[0,1],[1,0]] return = 2 The two open corner cells are diagonal neighbors. Example 2 grid = [[1,0],[0,0]] return = -1 The starting cell is blocked. Constraints 1 <= n <= 100 grid.length == grid[i].length == n Every cell is 0 or 1.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Every move costs one cell, so the graph is unweighted and BFS gives the shortest path the first time it reaches the target. Start the queue at (0,0) with distance 1, because the answer counts cells, not edges. Check the start and end cells first. If either is 1, return -1 right away. Example 2 is exactly this case. Mark cells visited when you push them, not when you pop them, or the queue blows up with duplicates. Loop over all eight direction offsets and bounds-check each one. The n = 1 grid with a 0 returns 1, so handle it without special logic by checking the target when you pop. The classic pitfall is using DFS, which finds a path but not the shortest one. With n up to 100, BFS touches at most 10,000 cells, so there's no performance trap. If you freeze on the live OA, StealthCoder can hand you the clean BFS template.
Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.
You can drill Shortest Path in a Binary Matrix cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Made for the candidate who got the OA invite this morning and has 72 hours, not six months.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as shortest path in binary matrix. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Meta's OA.
Meta reuses patterns across OAs. Made for the candidate who got the OA invite this morning and has 72 hours, not six months. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Shortest Path in a Binary Matrix FAQ
How hard is Shortest Path in a Binary Matrix really?+
It's a medium that feels easy once you spot BFS. The grid is the graph and every step costs the same. Most failures come from edge cases, not the algorithm: a blocked start, a blocked end, or a 1x1 grid. Get those right and it's mechanical.
What's the trick to this Meta OA question?+
Treat it as unweighted shortest path and use BFS with eight directions. Start the distance at 1 since the answer counts cells. Mark cells visited when you enqueue them. That's the whole trick. No heap, no Dijkstra, no DP.
Why not use DFS here?+
DFS finds a path, not the shortest one. It would explore long winding routes first and you'd need to track the minimum across all paths, which gets exponential. BFS expands level by level, so the first time it hits the bottom-right cell is the minimum.
Which edge cases should I test before submitting?+
Test a blocked top-left cell, a blocked bottom-right cell, and a 1x1 grid containing 0, which should return 1. Also test a fully open 2x2 grid, where the diagonal gives 2. Finally try a grid with no route and confirm you return -1.
How do I prepare for this in 48 hours?+
Write BFS on a grid from scratch twice. Use a deque, a direction array, and a visited check on enqueue. Then change the move set from four directions to eight. Time yourself. If you can do it in under 15 minutes, you're ready for this Meta question.