Enumerate Right-and-Down Matrix Paths
Reported by candidates from Oracle's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Oracle reported this one in July 2026, and the title sounds harmless: enumerate right-and-down paths through a binary grid. The trap is the corner cases. A 1-by-1 grid returns a list holding one empty string, not an empty list, and that's where a naive solution falls over. Hint says BFS, but this is really path enumeration with DFS or backtracking, pruned by blocked cells. If you've got the OA in a day or two, learn the edge cases first. StealthCoder is the safety net if you freeze during the live assessment, but you should know the shape of this one before you sit down.
The problem
You are given a non-empty rectangular binary matrix grid. A cell containing 1 is valid, and a cell containing 0 is blocked. Enumerate every valid path from the top-left cell to the bottom-right cell. Each move must go exactly one cell Down or one cell Right, and every visited cell must contain 1. Represent a path as a string containing 'D' and 'R'. Return all path strings in lexicographic order. If either endpoint is blocked or no valid path exists, return an empty list. A valid 1-by-1 matrix has one path represented by the empty string. Function enumeratePaths(grid: int[][]) → List<String> Examples Example 1 grid = [[1,1,1],[1,1,1]] return = ["DRR","RDR","RRD"] All three monotone paths stay on valid cells. Because 'D' precedes 'R', their lexicographic order is "DRR", "RDR", then "RRD". Example 2 grid = [[1,0],[1,1]] return = ["DR"] The first Right move is blocked, so the only path moves Down and then Right. Example 3 grid = [[1]] return = [""] The start is already the destination. The unique path contains no moves, so it is represented by the empty string. Example 4 grid = [[1,0],[0,1]] return = [] Both possible first moves enter blocked cells, so no path reaches the destination. Constraints 1 <= grid.length <= 100 and 1 <= grid[i].length <= 100. Every row has the same length. Every cell is either 0 or 1. The total number of valid paths is at most 10000.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The core idea is backtracking from (0,0) with a path buffer. Try 'D' before 'R' at each step, and the output comes out in lexicographic order for free, so you never need a sort. Stop at the bottom-right cell and record the current string. The pitfalls are all at the edges. If the start or end cell is 0, return an empty list immediately. If the grid is 1-by-1 and the cell is 1, return a list containing the empty string. Don't confuse [] with [""]. The pruning matters too. Dead ends with no route to the end can waste time, so a reachability table computed backward from the destination keeps the search tight. The cap of 10000 paths keeps output manageable. Moves only go down or right, so there's no need for a visited set. StealthCoder is the hedge if the live OA wipes your memory of the base case.
The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.
You can drill Enumerate Right-and-Down Matrix 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 for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Oracle's OA.
Oracle reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Enumerate Right-and-Down Matrix Paths FAQ
What's the trick to this Oracle path enumeration problem?+
Run DFS with backtracking and try 'D' before 'R'. That ordering gives lexicographic output without sorting. Skip any cell that is 0 or out of bounds. Append the path string when you hit the bottom-right cell. Since moves only go down or right, you never need a visited set.
Why does the 1-by-1 grid return [""] and not []?+
The start is already the destination, so exactly one path exists and it has zero moves. That path is the empty string. Returning an empty list would claim no path exists. This is the edge case that breaks naive solutions, so test it first.
Is BFS or DFS better here?+
DFS with backtracking is the cleaner fit. BFS can enumerate paths but forces you to carry a path string in every queue entry and manage ordering yourself. DFS building a path buffer and trying 'D' first is simpler, and lexicographic order falls out naturally.
Do I need to worry about performance with a 100 by 100 grid?+
Mildly. The output is capped at 10000 paths, but blind DFS can still wander into many dead ends. Precompute which cells can reach the bottom-right with a backward DP pass, then only step into cells that can. That prunes the search so work stays close to the output size.
How do I prepare for this in 48 hours?+
Write the DFS version from scratch twice. Then run the four examples by hand, especially the 1-by-1 and blocked-endpoint cases. Practice the backward reachability pruning once. That's enough to handle this pattern and its common variants, such as counting paths or unique paths with obstacles.