All Simple Paths in an Undirected Graph
Reported by candidates from Bloomberg's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
A Bloomberg OA reported in November 2019 asks for every simple path between two nodes in an undirected graph. Your first instinct is to worry about the blowup, and you should. The constraint n <= 15 is the tell: it says exponential enumeration is expected, so nobody wants a clever polynomial answer. This is graph DFS with backtracking, output in sorted lexicographic order. If you freeze on the visited-set bookkeeping during the live assessment, StealthCoder runs invisibly as a safety net and hands you a working solution. Know the shape first, though. It's short once you see it.
The problem
Build an undirected graph with nodes 0..n-1 from edges. Return every simple path from source to target. A simple path repeats no node. Sort each adjacency list and return paths in depth-first lexicographic order. Function allSimplePaths(n: int, edges: int[][], source: int, target: int) → int[][] Examples Example 1 n = 4 edges = [[0,1],[1,3],[0,2],[2,3],[1,2]] source = 0 target = 3 return = [[0,1,2,3],[0,1,3],[0,2,1,3],[0,2,3]] DFS follows sorted neighbors and never repeats a node. Constraints 1 <= n <= 15. Edges are unique undirected pairs. Source and target are valid distinct nodes.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Build an adjacency list from edges, add both directions, and sort each list. Then run DFS from source with a current path and a visited set. When you reach target, copy the path into the results and return. Otherwise loop over sorted neighbors, skip visited ones, push, recurse, pop, and unmark. Sorted neighbors give you the required depth-first lexicographic order for free, so no final sort is needed. The common pitfalls: appending the live path list instead of a copy, so every result mutates into the same list. Forgetting to unmark on backtrack. Forgetting to sort adjacency lists. Also, stop at target and don't continue past it, since a simple path can't revisit nodes anyway. With n <= 15, the worst case is huge for dense graphs, but that's inherent to the output size. If you blank mid-assessment, StealthCoder is the hedge that keeps you moving.
Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.
You can drill All Simple Paths in an Undirected 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 for the candidate who got the OA invite this morning and has 72 hours, not six months.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as all paths from source to target. If you have time before the OA, drill that.
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.
All Simple Paths in an Undirected Graph FAQ
What's the trick in the Bloomberg all simple paths problem?+
It's DFS with backtracking. Keep a path list and a visited set, add a node, recurse, then remove it. Sort each adjacency list up front so the output order matches the required lexicographic order without a final sort.
Why is n <= 15 such a big hint?+
It tells you the answer can be exponential in size. A dense graph can have a factorial number of simple paths, so brute-force enumeration is the intended solution. Don't hunt for dynamic programming or a shortcut that counts paths, because you must return them all.
What's the most common bug on this one?+
Appending the path list itself to results instead of a copy. Every stored path then changes as you backtrack and you end up with garbage. Use path[:] or new ArrayList<>(path) at the moment you hit the target.
Do I need a visited set if I check the path?+
Yes, use a boolean array or set. Scanning the path list for membership works at n=15 but is sloppy. Mark on entry, unmark on exit, and you get O(1) checks and cleaner backtracking.
How do I prepare for this in 48 hours?+
Write the DFS backtracking template from memory twice. Test on the sample graph with a cycle, like the 1-2 edge, to confirm no node repeats. Then practice a couple of similar graph path enumeration problems so the pattern feels automatic.