Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Home
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets

Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Back to Problems

797. All Paths From Source to Target

Medium82.8% Acceptance
BacktrackingDepth-First SearchBreadth-First Search
Asked by:
Bloomberg
ProblemSolutions (12)VideosCompanies (8)Notes

Problem Statement

Given a directed acyclic graph (DAG) of n nodes labeled from 0 to n - 1, find all possible paths from node 0 to node n - 1 and return them in any order.

The graph is given as follows: graph[i] is a list of all nodes you can visit from node i (i.e., there is a directed edge from node i to node graph[i][j]).

Example 1:

Input: graph = [[1,2],[3],[3],[]]
Output: [[0,1,3],[0,2,3]]
Explanation: There are two paths: 0 -> 1 -> 3 and 0 -> 2 -> 3.

Example 2:

Input: graph = [[4,3,1],[3,2,4],[3],[4],[]]
Output: [[0,4],[0,3,4],[0,1,3,4],[0,1,2,3,4],[0,1,4]]

Constraints:

  • n == graph.length
  • 2 <= n <= 15
  • 0 <= graph[i][j] < n
  • graph[i][j] != i (i.e., there will be no self-loops).
  • All the elements of graph[i] are unique.
  • The input graph is guaranteed to be a DAG.
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
B
G
Google
A
Adobe
A
Amazon
F
Facebook
+3

Approach

The problem asks us to return all possible paths from node 0 (source) to node n-1 (target) in a directed acyclic graph represented as an adjacency list. Since we must explore every valid path, the most natural technique is Depth-First Search (DFS) with backtracking.

Start from node 0 and recursively explore each neighbor while maintaining the current path. Whenever the traversal reaches the target node, record the path as a valid result. Backtracking ensures that after exploring one branch, the algorithm removes the last node and continues exploring alternative routes. Because the graph is acyclic, we do not need complex cycle detection.

An alternative is a Breadth-First Search (BFS) that stores partial paths in a queue and expands them level by level until reaching the destination.

If there are P valid paths and each path has length up to N, the total work is proportional to exploring and storing these paths. Thus, the time complexity grows with the number of possible paths.

Complexity

ApproachTime ComplexitySpace Complexity
DFS with BacktrackingO(P × N) where P is number of paths and N is path lengthO(N) recursion stack (excluding output)
BFS Path ExpansionO(P × N)O(P × N) for storing paths in queue

Video Solution Available

AlgosWithMichael

View all video solutions

Solutions (12)

Depth First Search (DFS) Approach

The Depth First Search (DFS) approach uses a recursive function to explore all paths from the source node to the target node by traversing each possible node in a depth-first manner. This involves exploring as far as possible along each branch before backing up.

Time Complexity: O(2^n) - since every node could lead to every other node, creating exponential paths.
Space Complexity: O(n) - the maximum depth of the recursion, and path storage could be O(n) as well.

CC++JavaPythonC#JavaScript
1#include <stdio.h>
2#include <stdlib.h>
3
4void dfs(int node, int n, int** graph, int* graphColSize,

Explanation

This C solution implements a depth-first search (DFS) recursively. The function "dfs" is called with each node and adds the node to the current path. If the current node is the target node, the path is added to the result. Otherwise, the function is called recursively for each of the node's children. Memory is dynamically allocated for the paths and adjusted for recursion depth.

Breadth First Search (BFS) Approach

The Breadth First Search (BFS) approach uses a queue to explore nodes level by level, tracking each path as it progresses. This is an iterative process that can make finding all possible paths easier to visualize compared to recursive methods like DFS.

Time Complexity: O(2^n) - Because each node may lead to different paths creating an exponential number of routes.
Space Complexity: O(2^n) - Required for holding all potential paths in the queue.

CC++JavaPythonC#JavaScript
1





Video Solutions

Watch expert explanations and walkthroughs

Graph Coding Question - All Paths From Source To Target (LeetCode)

AlgosWithMichael
12:3941,042 views

Asked By Companies

8 companies
B
Bloomberg
G
Google
A
Adobe
A
Amazon
F
Facebook
M
Microsoft
A
Apple
Y
Yahoo

Prepare for Interviews

Practice problems asked by these companies to ace your technical interviews.

Explore More Problems

Notes

Personal Notes

Jot down your thoughts, approach, and key learnings

0 characters

Similar Problems

Path Sum IIMedium
Binary Tree PathsEasy
Smallest String Starting From LeafMedium
More similar problems

Related Topics

BacktrackingDepth-First SearchBreadth-First SearchGraph

Problem Stats

Acceptance Rate82.8%
DifficultyMedium
Companies8

Practice on LeetCode

Solve with full IDE support and test cases

Solve Now

Frequently Asked Questions

Why does this problem commonly use DFS instead of BFS?

DFS is preferred because it naturally explores one path completely before trying alternatives, which aligns well with generating full paths. BFS can also work, but it requires storing many partial paths in memory, making it less space-efficient in many cases.

Is All Paths From Source to Target asked in FAANG interviews?

Yes, variations of this problem appear in technical interviews at large tech companies. Interviewers often use it to test graph traversal concepts, recursion, and the candidate's ability to generate all possible solutions using DFS or backtracking.

What data structure is best for All Paths From Source to Target?

An adjacency list is the best representation for the graph because it allows efficient traversal of neighbors. During DFS, a dynamic list or vector is used to track the current path, and recursion naturally manages exploration and backtracking.

What is the optimal approach for All Paths From Source to Target?

The most common approach is Depth-First Search with backtracking. Starting from node 0, we explore each neighbor recursively while maintaining the current path. When we reach node n-1, we add the path to the result and backtrack to explore other possibilities.

Previous Problem

Letter Case Permutation

int
*
path
,
int
pathLen
,
int
*
*
result
,
int
*
returnSize
,
int
*
*
returnColumnSizes
)
{
5
path
[
pathLen
++
]
=
node
;
6
if
(
node
==
n
-
1
)
{
7
result
[
*
returnSize
]
=
malloc
(
pathLen
*
sizeof
(
int
)
)
;
8
for
(
int
i
=
0
;
i
<
pathLen
;
i
++
)
9
result
[
*
returnSize
]
[
i
]
=
path
[
i
]
;
10
(
*
returnColumnSizes
)
[
*
returnSize
]
=
pathLen
;
11
(
*
returnSize
)
++
;
12
}
else
{
13
for
(
int
i
=
0
;
i
<
graphColSize
[
node
]
;
i
++
)
14
dfs
(
graph
[
node
]
[
i
]
,
n
,
graph
,
graphColSize
,
path
,
pathLen
,
result
,
returnSize
,
returnColumnSizes
)
;
15
}
16
}
17
18
int
*
*
allPathsSourceTarget
(
int
*
*
graph
,
int
graphSize
,
int
*
graphColSize
,
int
*
returnSize
,
int
*
*
returnColumnSizes
)
{
19
int
*
*
result
=
malloc
(
1000
*
sizeof
(
int
*
)
)
;
20
*
returnColumnSizes
=
malloc
(
1000
*
sizeof
(
int
)
)
;
21
int
*
path
=
malloc
(
15
*
sizeof
(
int
)
)
;
22
*
returnSize
=
0
;
23
dfs
(
0
,
graphSize
,
graph
,
graphColSize
,
path
,
0
,
result
,
returnSize
,
returnColumnSizes
)
;
24
free
(
path
)
;
25
return
result
;
26
}
#
include
<stdio.h>
2
#
include
<stdlib.h>
3
#
include
<string.h>
4
5
#
define
MAX_NODE
15
6
#
define
MAX_PATHS
2048
7
8
int
*
*
allPathsSourceTarget
(
int
*
*
graph
,
int
graphSize
,
int
*
graphColSize
,
int
*
returnSize
,
int
*
*
returnColumnSizes
)
{
9
int
*
*
result
=
malloc
(
MAX_PATHS
*
sizeof
(
int
*
)
)
;
10
*
returnColumnSizes
=
malloc
(
MAX_PATHS
*
sizeof
(
int
)
)
;
11
*
returnSize
=
0
;
12
13
int
queue
[
MAX_PATHS
]
[
MAX_NODE
]
;
14
int
front
=
0
,
rear
=
0
;
15
int
pathLens
[
MAX_PATHS
]
;
16
17
memcpy
(
queue
[
rear
]
,
(
int
[
]
)
{
0
}
,
sizeof
(
int
)
)
;
18
pathLens
[
rear
++
]
=
1
;
19
20
while
(
front
<
rear
)
{
21
int
*
curPath
=
queue
[
front
]
;
22
int
curLen
=
pathLens
[
front
++
]
;
23
int
lastNode
=
curPath
[
curLen
-
1
]
;
24
25
if
(
lastNode
==
graphSize
-
1
)
{
26
result
[
*
returnSize
]
=
malloc
(
curLen
*
sizeof
(
int
)
)
;
27
memcpy
(
result
[
*
returnSize
]
,
curPath
,
curLen
*
sizeof
(
int
)
)
;
28
(
*
returnColumnSizes
)
[
*
returnSize
]
=
curLen
;
29
(
*
returnSize
)
++
;
30
}
else
{
31
for
(
int
i
=
0
;
i
<
graphColSize
[
lastNode
]
;
i
++
)
{
32
memcpy
(
queue
[
rear
]
,
curPath
,
curLen
*
sizeof
(
int
)
)
;
33
queue
[
rear
]
[
curLen
]
=
graph
[
lastNode
]
[
i
]
;
34
pathLens
[
rear
++
]
=
curLen
+
1
;
35
}
36
}
37
}
38
return
result
;
39
}

Explanation

This C solution implements Breadth First Search (BFS) using a queue to iteratively explore the graph level by level. Paths are stored in the queue and processed in FIFO order. New paths are generated for each child node, leading to either an extension to the path or storing the path if it reaches the target node.