Reported July 2022
Bloombergbacktracking

All Paths From Source to Target

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 reported this one in July 2022, and the constraint is the whole story: n is at most 15. That tiny size tells you the output is the point, not clever pruning. You're enumerating every path from node 0 to node n - 1 in a DAG, in depth-first order. If your OA invite lands this week, expect a plain DFS with backtracking. No visited set, no memo, no shortest-path machinery. StealthCoder sits invisible on your screen as a safety net if you blank on the recursion, but this one is short enough to own before you open the assessment.

The problem

Given a directed acyclic graph as an adjacency list graph, return every path from node 0 to node n - 1.
Each returned path includes both endpoints. Visit outgoing neighbors in their listed order and return paths in the resulting depth-first order.

Function
allPathsSourceTarget(graph: int[][]) → int[][]

Examples
Example 1
graph = [[1,2],[3],[3],[]]
return = [[0,1,3],[0,2,3]]
The two directed routes go through node 1 or node 2.
Example 2
graph = [[4,3,1],[3,2,4],[3],[4],[]]
return = [[0,4],[0,3,4],[0,1,3,4],[0,1,2,3,4],[0,1,4]]
Depth-first traversal follows each adjacency list in its supplied order.

Constraints
2 <= graph.length <= 15.
Every neighbor is a valid node index and the graph is acyclic.
The input contains no duplicate outgoing edge.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is that the graph is acyclic, so you never need a visited set. Start at node 0 with a path list, push the current node, and recurse into each neighbor in its listed order. When you hit node n - 1, copy the path into the results and return. Pop the node on the way back out. The classic pitfall is appending the live path list instead of a copy, so every answer ends up empty or identical. Another miss is adding a visited array, which breaks paths that share intermediate nodes. The order requirement is free if you iterate adjacency lists as given. With n capped at 15, the number of paths can be large, but that's the output size, not a flaw in your approach. If the recursion slips under pressure, StealthCoder can hand you the backtracking skeleton live.

Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.

If this hits your live OA

You can drill All Paths From Source to Target 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 by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge.

Get StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as all paths from source to target. If you have time before the OA, drill that.

⏵ The honest play

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

Bloomberg reuses patterns across OAs. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge. Works on HackerRank, CodeSignal, CoderPad, and Karat.

All Paths From Source to Target FAQ

What's the trick to All Paths From Source to Target?+

Plain DFS with backtracking. Keep a running path, append the current node, recurse into each neighbor, and pop on return. When you reach node n - 1, store a copy of the path. Because the graph is acyclic, you don't need any visited tracking.

Why doesn't the input size need an optimized solution?+

Graph length is capped at 15, and the answer must list every path anyway. Runtime is dominated by output size, so you can't beat enumeration. Memoizing paths won't help much either, since you still have to build each full list.

What bug sinks most people on this problem?+

Pushing the shared path list into the results without copying it. Later pops mutate it and your answers come out wrong. Use a slice copy, list(path), or the equivalent in your language at the moment you reach the target.

Does the output order matter for the Bloomberg version?+

Yes. The problem says to visit neighbors in their listed order and return paths in the resulting depth-first order. Just iterate each adjacency list front to back and append results as you find them. Don't sort anything.

How do I prepare for this in 48 hours?+

Write the DFS backtracking solution from scratch twice, once recursive and once with an explicit stack if you want a backup. Test it on both examples by hand. Then check the empty-adjacency case, where a node has no outgoing edges and isn't the target.

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