FastPrepAll Simple Paths in a Cyclic Directed Graph

All Simple Paths in a Cyclic Directed Graph

Bloomberg LP logoBloomberg LP● MediumNEW GRADPHONE SCREENONSITE INTERVIEW
Learn

Problem statement

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 = 4edges = [[0,1],[1,2],[2,0],[1,3],[2,3],[0,3]]source = 0target = 3return = [[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.

More Bloomberg LP problems

See Bloomberg LP hiring insights
public int[][] allSimpleDirectedPaths(int n, int[][] edges, int source, int target) {
  // Write your code here.
}
n4
edges[[0,1],[1,2],[2,0],[1,3],[2,3],[0,3]]
source0
target3
expected[[0,1,2,3],[0,1,3],[0,3]]
Checking account…