Enumerate Grid Paths
Reported by candidates from Motive's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The Motive OA reported in August 2022 hands you a tiny grid and asks for every path as a string like "DRR". For a 1x1 grid the answer is a single empty string, which trips people before they write a line. It's path enumeration, and the hinted pattern is breadth-first search, though backtracking gets there faster. Rows and cols cap at 8, so the output stays small. If you blank on the ordering or the base case, StealthCoder sits invisibly on your screen during the live assessment and gives you a working solution. Still, this one is learnable in an evening.
The problem
Starting at the top-left cell of a rows-by-cols grid, move only down (D) or right (R) until the bottom-right cell. Return every path as a move string in lexicographic order. Function enumerateGridPaths(rows: int, cols: int) → String[] Examples Example 1 rows = 2 cols = 3 return = ["DRR","RDR","RRD"] Example 2 rows = 1 cols = 1 return = [""] Constraints 1 <= rows, cols <= 8.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is that you only have two moves, D and R, and you need exactly rows-1 downs and cols-1 rights. Recurse from (0,0), try D first, then R, and append the character to a path buffer. D sorts before R lexicographically, so trying D first gives sorted output with no extra sort. When you hit the bottom-right cell, push the current string. The common pitfall is the 1x1 case. You must return [""], not an empty list. Another miss is bounds checking: stop when row or col goes out of range. A BFS with a queue of (r, c, path) also works, but you'd need to keep order by expanding D before R. Total paths are C(r+c-2, r-1), at most 3432 here, so there's no performance worry. If the recursion gets tangled mid-assessment, StealthCoder is the hedge that gets you unstuck without anyone seeing it.
If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.
You can drill Enumerate 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. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Motive's OA.
Motive reuses patterns across OAs. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Enumerate Grid Paths FAQ
What's the trick to Enumerate Grid Paths?+
Every path has exactly rows-1 D moves and cols-1 R moves. Recurse with a path buffer, try D before R, and record the string when you reach the bottom-right cell. Since D sorts before R, the output comes out in lexicographic order automatically.
How hard is this one really?+
Easy to medium. The logic is a few lines of backtracking. The difficulty is in the details: the 1x1 case returning [""], bounds checks, and keeping the order right. If you've written any DFS that builds strings, you can finish it fast.
Do I need to sort the result?+
No. If you always explore D before R, paths are generated in lexicographic order. Sorting afterward still works and is cheap with at most 3432 paths, but it's unnecessary. Skipping it shows you understand why the order holds.
Should I use BFS or DFS?+
DFS with backtracking is simpler and uses less memory. BFS works if you queue (row, col, path) and expand D before R, but the final order needs care. The hint says BFS, though both are accepted as long as the output matches the examples.
How do I prepare for this in 48 hours?+
Write the recursive version from scratch twice. Test 1x1, 1xN, Nx1, and 2x3 against the examples. Then practice similar path enumeration like generating parentheses so the buffer-append, recurse, undo pattern feels automatic.