Talentd
FleetCode
Make and follow sheets.Sign in

Depth-First Search Problems (338)

Problems tagged with Depth-First Search. Start with the easy ones, then move to medium and hard.

39 Easy208 Medium91 Hard

Learn these first

1
Tree

Tree traversal problems like preorder, inorder, and postorder are classic DFS applications. These problems build intuition for recursive exploration patterns.

2
Graph

DFS is most commonly applied to graphs. Understanding adjacency lists, edges, and traversal helps you implement DFS for connectivity, cycle detection, and component discovery.

3
Stack

DFS can be implemented iteratively using a stack. Understanding stack behavior helps simulate recursion and optimize traversal in large graphs.

4
Recursion

Most DFS implementations rely on recursion. Learning recursion helps you understand call stacks, base cases, and how DFS naturally explores deep branches.

5
Breadth-First Search

Comparing DFS with BFS helps you choose the right traversal strategy. Many interview problems can be solved with either approach depending on constraints.

TitleSolutionAcceptDifficultyTopics
269. Alien DictionaryVideoPremium
Solution37.1%Hard
Array, Breadth-First Search +4· 18 companies
Solution49.9%Hard
Array, Depth-First Search +3· 3 companies
499. The Maze IIIVideoPremium
Solution52.3%Hard
Array, Breadth-First Search +6· 1 company
749. Contain VirusVideo
Solution56.5%Hard
Array, Breadth-First Search +3· 3 companies
Solution68.3%Hard
Array, Binary Search +5· 8 companies
Solution57.0%Hard
Array, Breadth-First Search +3· 16 companies
Solution56.7%Hard
Array, Breadth-First Search +4· 5 companies
Solution43.6%Hard
Array, Breadth-First Search +4· 10 companies
Solution48.3%Hard
Binary Tree, Depth-First Search +2· 14 companies
Solution37.0%Hard
Array, Breadth-First Search +2· 4 companies
1516. Move Sub-Tree of N-Ary TreeVideoPremium
–59.7%Hard
1766. Tree of CoprimesVideo
Solution45.1%Hard
Array, Depth-First Search +3· 1 company
–66.5%Hard
–62.1%Hard
Array, Breadth-First Search +2· 1 company
Solution43.8%Hard
Array, Depth-First Search +2· 2 companies
Showing 1–50 of 88

More about Depth-First Search

Depth-First Search (DFS) is one of the most fundamental traversal techniques used in data structures and algorithms. It explores a graph or tree by going as deep as possible along each branch before backtracking. DFS is commonly implemented using recursion or an explicit stack and is widely used to traverse Graph and Tree structures.

In coding interviews, DFS appears frequently because it naturally models many search and exploration problems. Whether you're checking connectivity in a graph, exploring all paths in a grid, or validating a binary tree property, DFS is often the simplest and most intuitive approach. Top tech companies expect candidates to be comfortable with DFS-based reasoning, especially when combined with Recursion, Backtracking, and other traversal strategies like Breadth-First Search.

As you practice DFS problems, you'll start to recognize common patterns that appear repeatedly in interview questions. Some of the most important DFS techniques include:

  • Recursive traversal for trees and graphs
  • Backtracking exploration for generating combinations or paths
  • Visited set tracking to avoid cycles in graphs
  • Connected component discovery in matrices or adjacency lists
  • Path and state exploration for constraint-based problems

DFS is particularly useful when the solution requires exploring every possible path or deeply analyzing a structure before moving to the next branch. It powers algorithms for cycle detection, topological ordering, strongly connected components, and many grid-based search problems.

On FleetCode, you can master this technique through 333 carefully curated Depth-First Search problems. These exercises range from beginner tree traversals to advanced graph exploration challenges, helping you build strong intuition and interview-ready problem-solving skills.

Depth-First Search: common questions

What are common Depth-First Search patterns?

Common DFS patterns include recursive tree traversal, graph exploration with visited sets, connected component detection, backtracking for path generation, and DFS with memoization for optimization problems.

What is the best way to learn Depth-First Search?

Start by understanding recursive traversal on trees, then practice DFS on graphs and grid-based problems. Focus on recognizing patterns such as visited tracking, recursion depth, and backtracking exploration.

What is the time complexity of Depth-First Search?

For graphs represented with adjacency lists, DFS runs in O(V + E) time where V is the number of vertices and E is the number of edges. The space complexity is typically O(V) due to recursion stack or visited storage.

Is Depth-First Search important for FAANG interviews?

Yes. DFS is a core technique tested frequently in FAANG and other top tech interviews. It appears in graph traversal, tree recursion, backtracking problems, and grid exploration questions.

What are the best Depth-First Search problems for interviews?

The best DFS interview problems typically involve tree traversal, graph connectivity, island counting in grids, path finding, and backtracking. Classic examples include Number of Islands, Path Sum, Course Schedule variants, and graph cycle detection.