Reported December 2020
Bloombergbacktracking

All Simple Paths in a Cyclic Directed Graph

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 n <= 15 cap on this Bloomberg OA, reported December 2020, is the whole hint. It tells you exponential enumeration is expected, so nobody wants a clever polynomial trick. You're listing every simple path from source to target in a directed graph that has cycles. That's DFS with backtracking and a visited set. Sort the adjacency lists first so the output order matches. The cycle in the example is there to punish anyone who forgets to mark nodes. If you blank on the backtracking shape during the live assessment, StealthCoder is the invisible safety net that reads the problem and hands you the solution.

The problem

Build a directed graph with nodes 0..n-1 from edges. Return every simple path from source to target.
A simple path repeats no node. Sort adjacency lists and return paths in depth-first lexicographic order.

Function
allSimpleDirectedPaths(n: int, edges: int[][], source: int, target: int) → int[][]

Examples
Example 1
n = 4
edges = [[0,1],[1,2],[2,0],[1,3],[2,3],[0,3]]
source = 0
target = 3
return = [[0,1,2,3],[0,1,3],[0,3]]
The visited-path set prevents cycling through 0,1,2.

Constraints
1 <= n <= 15.
Directed edges are unique.

Reported by candidates. Source: FastPrep

Pattern and pitfall

Build adjacency lists from edges, then sort each one ascending. Run DFS from source with a current path list and a visited array. On entering a node, mark it and push it. If it equals target, copy the path into results and return. Otherwise recurse into each unvisited neighbor, then unmark and pop on the way out. Sorted neighbors give you the depth-first lexicographic order for free, no final sort needed. The common pitfalls: appending the path reference instead of a copy, forgetting to unmark on backtrack, and continuing past target (a simple path ends the moment it hits target, since revisiting it is illegal anyway). Also handle source equal to target, which gives a single path of one node. Worst case is factorial-ish, which is fine at n = 15. StealthCoder is your hedge if the unmark step slips your mind mid-OA.

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 Simple Paths in a Cyclic Directed Graph 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

⏵ 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 Simple Paths in a Cyclic Directed Graph FAQ

What's the trick in this Bloomberg graph problem?+

DFS with backtracking. Keep a visited set for the current path only, mark on entry, unmark on exit. The cycles in the graph never cause infinite loops because a node already on the path can't be revisited. Sort adjacency lists up front for the required order.

Why is n capped at 15?+

The number of simple paths can blow up exponentially or worse in dense graphs. A cap of 15 signals that full enumeration is intended and fast enough. Don't hunt for memoization or DP, since you must output every path anyway.

How do I get the lexicographic order right?+

Sort each node's adjacency list ascending before the DFS. Because you explore neighbors in that order and emit paths when reaching target, the results come out in depth-first lexicographic order. No post-sort required, though sorting the final list also works.

What bugs sink most solutions here?+

Pushing the live path array into results without copying it, so every entry ends up empty or identical. Also forgetting to unmark visited on backtrack, which drops valid paths. Test with the example's 0,1,2 cycle to catch both fast.

How do I prepare for this in 48 hours?+

Write the all-paths DFS from scratch twice, once on a DAG and once with cycles. Add the visited array for the cyclic version. Check edge cases: source equals target, no path exists, and a single-node graph. That covers nearly everything this problem can throw at you.

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