Skip to content

Depth-First Search problems,
and the tell.

You need to reach everything, or ask a question about a whole branch before moving on. 190 problems in the catalogue carry this tag, from first-week easy to interview hard.

Free to start. Running and submitting a solution never costs credits.

How the pattern actually works.

One mechanism, and the mistake that costs people the problem.

The tell. You need to reach everything, or ask a question about a whole branch before moving on.

The mechanism. Go as deep as you can down one path, then come back and take the next. Recursion gives you the stack for free; an explicit stack does the same without risking a stack overflow.

What goes wrong. Marking a node seen when you take it out instead of when you put it in. On a graph with cycles that is an infinite walk.

The ones asked most often.

By how often the catalogue records them being set, not by our opinion.

  1. Binary Tree CamerasHardasked at 8 companies
  2. Number of Connected Components in an Undirected GraphMediumasked at 3 companies
  3. Difference Between Maximum and Minimum Price SumHardasked at 2 companies
  4. Maximum XOR of Two Non-Overlapping SubtreesHardasked at 2 companies
  5. Count Nodes Equal to Average of SubtreeMediumasked at 2 companies
  6. Path Sum IVMediumasked at 1 companies
  7. Island PerimeterEasyasked at 1 companies
  8. Detonate the Maximum BombsMediumasked at 1 companies
  9. Find All Groups of FarmlandMediumasked at 1 companies
  10. Maximize Sum of Weights after Edge RemovalsHardasked at 1 companies

Read it, watch it, then solve it.

The order that makes a pattern stick.

Everything in the app, in one place.

Practice, interviews and the tools, all free to start. Swipe to see the rest.

Free to start: 100 AI credits when you sign up and 100 every month. Start free in the app

About depth-first search problems.

Free to start, no card.

Start practising
How do I know a problem is a depth-first search problem?

You need to reach everything, or ask a question about a whole branch before moving on.

How many depth-first search problems should I solve?

Enough to stop needing the hint, which is usually ten to fifteen across the levels, not the whole list. The catalogue tags 190, split roughly 26 easy, 122 medium and 42 hard in the sample we show, so work up rather than across.

What goes wrong most often in depth-first search problems?

Marking a node seen when you take it out instead of when you put it in. On a graph with cycles that is an infinite walk.

Is practising by pattern better than a problem sheet?

A sheet tells you what to solve. A pattern tells you why those problems sit together, which is the part that carries over to the problem you have not seen. That is why the app files every problem under its pattern.