All Paths From Source to Target
Updated
Problem statement
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[][]) → List<List<Integer>>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.
More Bloomberg LP Onsite Interview questions
- N-ary Index-Path Encoder and DecoderPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Top K Frequent WordsONSITE INTERVIEW · Seen Nov 2026
- Trapping Rain WaterPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Word SearchOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Design Browser HistoryPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Mutable Stock Price Top KPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Add Two Numbers IIPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Decode StringPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Minimum Replacements to Make Two Strings AnagramsPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- Currency Conversion RatePHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
Similar questions at other companies
- Park Train Cars by PreferenceMercor · ONSITE INTERVIEWMercor interview questions
- Count Prefix Paths in a Character GraphGoogle · PHONE SCREENGoogle interview questions
- Directional Tiles: Find a Valid Reconfiguration PathDuolingo · PHONE SCREENDuolingo interview questions
- Attraction Route Without Reusing TrailsWhatNot · PHONE SCREENWhatNot interview questions
- Enumerate Directed Paths and CyclesByteDance · PHONE SCREENByteDance interview questions
- Course ScheduleAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Package Install OrderVerily · ONSITE INTERVIEW
- Task Build Order and File-Change RerunsVanta · PHONE SCREEN
- Course SchedulePlaud · ONSITE INTERVIEW
- All Halfway Courses Across Course TracksCrunchyroll · PHONE SCREENCrunchyroll interview questions