Skip to content

Depth First Search
Problems and Patterns

CodeStory tags 190 problems as Depth First Search, and 122 of them are Medium. Learn to go deep first, then solve the ten most asked.

Sign up at no cost to receive 100 AI credits, then 100 more monthly.

How the Depth First Search Pattern Works

CodeStory covers the tell, the stack mechanism and the visited mark mistake.

Spot the tell. You need to reach everything, or answer a question about a full branch before moving on. Study CodeStory's Depth First Search guide before you code.

Learn the mechanism. Go as deep as you can down one path, then come back and take the next. Recursion supplies the stack, and an explicit stack avoids a stack overflow on deep inputs. Practise both in CodeStory's recursion problems and stack problems.

Avoid the mistake. Forgetting to mark a node as seen loops forever on a graph with cycles, and marking it late pushes duplicates onto the stack. Test your marking on CodeStory's graph problems.

Most Asked Depth First Search Problems

CodeStory places the problems that most companies set first.

  1. Binary Tree CamerasHard8 companies
  2. Number of Connected Components in an Undirected GraphMedium3 companies
  3. Difference Between Maximum and Minimum Price SumHard2 companies
  4. Maximum XOR of Two Non-Overlapping SubtreesHard2 companies
  5. Count Nodes Equal to Average of SubtreeMedium2 companies
  6. Path Sum IVMedium1 company
  7. Island PerimeterEasy1 company
  8. Detonate the Maximum BombsMedium1 company
  9. Find All Groups of FarmlandMedium1 company
  10. Maximize Sum of Weights after Edge RemovalsHard1 company

Study, Watch and Solve Depth First Search

Follow CodeStory's order to make deep walks stick.

DFS Problems FAQs

Start free with no card, or ask a mentor on a free call.

Start Practising DFS
How do I know a problem is a DFS problem?

CodeStory looks for two signs. You need to reach everything, or answer a question about a full branch before moving on.

How many DFS problems should I solve?

CodeStory recommends ten to fifteen across the levels, which is enough to stop needing the hint. CodeStory shows all 190 tagged problems, split 26 Easy, 122 Medium and 42 Hard, so expect mostly Medium.

Which mistake costs the most in DFS problems?

Forgetting to mark a node as seen costs the most. On a graph with cycles, your search loops forever.

Should I use recursion or an explicit stack for DFS?

Recursion supplies the stack for you, and an explicit stack avoids a stack overflow on deep inputs. Choose recursion for clarity and a stack for very deep graphs.

Is practising by pattern better than a problem sheet?

Yes. Sheets rank problems in a row, while CodeStory's patterns explain what ties them together, and that carries over to problems you have not seen. CodeStory files every problem under its pattern.

Which DFS problems do companies ask most?

Binary Tree Cameras leads, asked at 8 companies in CodeStory's catalogue. Number of Connected Components in an Undirected Graph follows at 3 companies.

Is DFS practice free to start?

Yes. You begin free with no card, and CodeStory credits your account with 100 AI credits at signup and 100 more every month.

Want Guided DFS Practice? Get a Free Callback

Share your dream job with CodeStory, and a mentor lays out a plan.

  1. 1CodeStory studies your level, from raw beginner to placement ready.
  2. 2CodeStory describes online and campus batches, with timings that suit your plans.
  3. 3You decide. No pressure, no payment on the call.

Get a Free Callback

I am a
I want to learn

CodeStory dials your number only for this callback.