Reported November 2019
Bloombergbacktracking

Four-Direction Unique Grid 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

The mistake that sinks a first attempt on this Bloomberg OA, reported in November 2019, is reaching for BFS because the problem looks like a grid path question. It isn't. You need every simple path, not the shortest one, and paths can wander through all four directions. With at most 16 passable cells, the intended move is backtracking with a visited set. Get that wrong and you'll return only shortest routes or loop forever. StealthCoder sits invisibly on your screen as a safety net if you blank on the live assessment, but the pattern below is small enough to carry in your head.

The problem

In a binary grid, 1 is passable and 0 blocked. Return every simple path from the start cell to the end cell using Down, Left, Right, or Up moves. A simple path visits no cell more than once.
Encode paths with letters D, L, R, U and return them lexicographically.

Function
allGridPaths(grid: int[][], startRow: int, startCol: int, endRow: int, endCol: int) → String[]

Examples
Example 1
grid = [[1,1],[1,1]]
startRow = 0
startCol = 0
endRow = 1
endCol = 1
return = ["DR","RD"]
The two simple shortest paths use Down-Right and Right-Down.

Constraints
At most 16 cells are passable.
Start and end are valid grid coordinates.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is DFS with backtracking. From the current cell, try D, L, R, U in that fixed order. Mark the cell visited, recurse, then unmark on the way back. When you hit the end cell, append the built path string to the results. The pitfall is stopping at shortest paths. Example 1 only shows two shortest paths, but the statement says every simple path, so longer detours must count when the grid allows them. Another trap is forgetting to unmark visited cells, which silently drops valid paths. Trying moves in the order D, L, R, U gives you lexicographic output for free, since those letters are alphabetical. Sort at the end anyway if you're unsure. The 16 passable cell cap is your signal that exponential search is fine. If you freeze in the live OA, StealthCoder can hand you the backtracking skeleton, but you should know the visited and unvisit rhythm cold.

Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.

If this hits your live OA

You can drill Four-Direction Unique 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 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. 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.

Four-Direction Unique Grid Paths FAQ

Is this a BFS problem or a DFS problem?+

DFS with backtracking. BFS finds shortest paths, but this asks for every simple path through the grid. You need to explore, mark a cell visited, recurse, then unmark it so other routes can reuse that cell. The tag may hint BFS, but enumeration points to backtracking.

What's the trick to getting lexicographic order?+

Try moves in the order D, L, R, U. Those letters are already in alphabetical order, so a DFS that explores them in that sequence emits paths sorted. If you want a safety check, sort the result list before returning. It costs little at this size.

Why does the 16 passable cell limit matter?+

It tells you exponential time is acceptable. Enumerating all simple paths can blow up on big grids, but with at most 16 open cells the search space stays small. Don't waste time hunting for a polynomial solution or memoization here.

What's the most common bug on this question?+

Forgetting to unmark the visited cell after recursing. Without that, the first path poisons later ones and you miss valid answers. The second most common is returning only shortest paths. Check your output on a 2x2 grid of ones against the example.

How do I prepare in 48 hours?+

Write the backtracking template from memory twice: bounds check, blocked check, visited check, mark, recurse over four directions, unmark. Then test on the 2x2 example, a grid with a blocked cell, and a start equal to end case. That covers most of what this question tests.

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