Reported May 2022
Bloombergbacktracking

Enumerate Right-and-Down Matrix Paths

Reported by candidates from Bloomberg's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Bloomberg OA. Under 2s to a working solution.
Founder's read

Bloomberg reportedly served this one in May 2022, and it looks like a grid search problem until you read the move rules. Only Down and Right are allowed, so it's really path enumeration on a DAG, and the output has to come back in lexicographic order. That's the whole trick. If you've seen the classic rat-in-a-maze problem, you're most of the way there. The catch is the 100 by 100 grid with a cap of 10000 valid paths, which means naive backtracking can still burn time on dead ends. If you freeze on the live OA, StealthCoder runs invisibly as a safety net while you work.

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 trick is ordering. Since 'D' sorts before 'R', try Down first, then Right, in a DFS. Paths come out already sorted, so you never need a sort step. The pitfall is dead-end exploration. A grid can be 100 by 100 with a huge blocked region, and plain backtracking will wander into cells that can never reach the goal. Fix it with a reachability precompute. Run a backward pass from the bottom-right and mark cells that can reach it using only Down and Right moves. Then DFS only into marked cells, so every branch yields at least one path. Also handle the edge cases: a blocked start or end returns an empty list, and a 1 by 1 valid grid returns a list holding one empty string. Build the path in a shared buffer and push or pop characters. If the recursion or the pruning goes sideways mid-assessment, StealthCoder is the hedge that gives you a working solution.

If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.

If this hits your live OA

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 by an Amazon engineer who passed his OA cold and still thinks the filter is broken.

Get StealthCoder

Related leaked OAs

⏵ The honest play

You've seen the question. Make sure you actually pass Bloomberg's OA.

Bloomberg reuses patterns across OAs. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Enumerate Right-and-Down Matrix Paths FAQ

What's the trick to this Bloomberg matrix path problem?+

Do a DFS that tries Down before Right. Because 'D' is lexicographically smaller than 'R', results come out sorted with no extra sorting. Add a backward reachability check so you never walk into cells that can't reach the bottom-right corner.

Is this really a BFS problem?+

The hint says BFS, but DFS with backtracking fits better. You need every path as a string in sorted order, and DFS with Down-first ordering gives that naturally. BFS would force you to carry partial path strings per cell and sort later.

What edge cases break most solutions?+

A blocked start or end must return an empty list. A 1 by 1 valid grid must return a list with one empty string, not an empty list. Those two cases are shown in the examples and are easy to get wrong when you write the base case.

How bad is the complexity with a 100 by 100 grid?+

Without pruning, dead-end branches can explode. With a reachability precompute in O(rows times cols), every DFS branch leads to a real path. Total work is then roughly the number of paths times the path length, and the cap is 10000 paths.

How do I prepare for this in 48 hours?+

Write the Down-first DFS with a shared path buffer once, then add the reachability grid. Test it on the four examples, especially the 1 by 1 and blocked-diagonal cases. That covers the pattern, and one clean run is enough for this one.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Bloomberg.

OA at Bloomberg?
Invisible during screen share
Get it