Enumerate Top-to-Bottom Grid Paths
Reported by candidates from Google's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Google flagged this one in July 2025, and the title makes it sound harder than it is. Strip the wording and it's plain backtracking DFS on a tiny grid. Start from each open top-row cell, walk down, right, left, up, never reuse a cell, and record the path the moment you hit the bottom row. The grid is at most 6x6 with at most 16 open cells, so brute force is the intended answer. If you freeze on the ordering rules or the stop condition during the live OA, StealthCoder is the safety net running invisibly on your screen.
The problem
You are given a rectangular binary matrix grid. A cell with value 0 is open, and a cell with value 1 is blocked. A path may start at any open cell in the top row. At each step, it may move one cell down, right, left, or up, staying inside the matrix and entering only open cells. A path is simple: it may not visit the same cell more than once. The path ends immediately when it first reaches the bottom row. Return every distinct valid path. Represent a cell as [row, column] and a path as its ordered list of cells. Return paths in deterministic depth-first-search order: Consider top-row starting cells from left to right. From each cell, try neighbors in the order down, right, left, then up. Function enumerateTopToBottomPaths(grid: int[][]) → int[][][] Examples Example 1 grid = [[0,0],[0,0]] return = [[[0,0],[1,0]],[[0,0],[0,1],[1,1]],[[0,1],[1,1]],[[0,1],[0,0],[1,0]]] Both top-row cells are valid starts. Depth-first search uses down, right, left, then up, and stops each path as soon as it reaches row 1. Example 2 grid = [[0,1],[1,1]] return = [] The only open top-row cell has no open route to the bottom row, so there are no valid paths. Example 3 grid = [[0,1,0]] return = [[[0,0]],[[0,2]]] In a one-row matrix, every open top-row cell is already in the bottom row. Starts are returned from left to right. Constraints 1 <= grid.length <= 6 1 <= grid[i].length <= 6 Every row has the same length. Every cell is either 0 or 1. There are at most 16 open cells. The input has at most 10000 valid paths. A path may not repeat a cell and ends on its first bottom-row cell.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is that this is enumeration, not search for a shortest path, so BFS is the wrong instinct even though the hint says it. Use recursive DFS with a visited set and a current path list. Push the cell, mark it visited, and if its row is the last row, copy the path into results and return immediately. Otherwise try neighbors in the exact order down, right, left, up. Then unmark and pop on the way back. The common pitfalls are forgetting to copy the path before storing it, continuing past the bottom row, and getting direction order wrong, which scrambles the output. A one-row grid is the edge case: every open top cell is already a finished path. With at most 10000 valid paths, complexity is fine. If you blank on the backtrack cleanup during the live OA, StealthCoder can hand you the clean version.
Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.
You can drill Enumerate Top-to-Bottom Grid Paths 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
You've seen the question.
Make sure you actually pass Google's OA.
Google 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.
Enumerate Top-to-Bottom Grid Paths FAQ
What's the trick in this Google OA problem?+
It's backtracking DFS, not BFS. Mark a cell visited, recurse in the order down, right, left, up, and unmark on return. Stop and record the path as soon as you reach the bottom row. The ordering rule decides whether your output matches.
How hard is this really?+
Easy to medium. The grid is at most 6x6 with at most 16 open cells, so no optimization is needed. The difficulty is clean bookkeeping: copying paths, undoing visited marks, and stopping at the first bottom-row cell.
Why does the order of results matter?+
The expected output is in deterministic DFS order. Starts go left to right across the top row, and neighbors are tried down, right, left, up. If you change the direction order, your paths are valid but appear in the wrong sequence and fail the check.
What edge cases should I test?+
A one-row grid, where each open top cell is a single-cell path. A grid where the open top cell is walled off, returning an empty list. Also test a top row with blocked cells so you skip them as starts.
How do I prepare in 48 hours?+
Write grid backtracking from scratch twice: visited set, path list, push and pop. Practice copying the path at the base case. Then run the three examples by hand. Word Search and Unique Paths III style problems use the same skeleton.