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.
- Binary Tree CamerasHard8 companies
- Number of Connected Components in an Undirected GraphMedium3 companies
- Difference Between Maximum and Minimum Price SumHard2 companies
- Maximum XOR of Two Non-Overlapping SubtreesHard2 companies
- Count Nodes Equal to Average of SubtreeMedium2 companies
- Path Sum IVMedium1 company
- Island PerimeterEasy1 company
- Detonate the Maximum BombsMedium1 company
- Find All Groups of FarmlandMedium1 company
- Maximize Sum of Weights after Edge RemovalsHard1 company
Study, Watch and Solve Depth First Search
Follow CodeStory's order to make deep walks stick.
More to Practise Inside the App
CodeStory pairs patterns with interviews and career tools. Swipe for more.
Technique Drills
Drill one technique before you move to the next.
352 problemsOpenHiring Company Sets
Practise what a named company asks.
463 companiesOpenLive Coding Interview
Choose a role and code while questions arrive.
scored reportOpenVoice Interview Rounds
Speak your answers and meet follow ups.
32 topicsOpenDBMS Viva Prep
Face index, join and normalisation questions.
in Core CSOpenOOP Viva Prep
Defend your inheritance and polymorphism answers.
in Core CSOpenSystem Design Chapters
Study how systems get built, layer by layer.
27 chaptersOpenSQL Challenges
Query real tables and check every row.
39 questionsOpenAptitude Tests
Quant, logic, verbal and DI under a timer.
280 questionsOpenTimed Coding Assessments
Practise with marks and hidden tests.
37 papersOpenAlgorithm Animations
Follow every step on input you choose.
26 algorithmsOpenGuided AI Hints
Reveal hints gradually before you ask for review.
100 free creditsOpenResume Maker
Build your resume beside your practice record.
in the appOpenReadiness Score
See one score and your weakest area.
tracked dailyOpen
Sign up to collect 100 AI credits, then 100 more monthly. Start free in the app
DFS Problems FAQs
Start free with no card, or ask a mentor on a free call.
Start Practising DFSHow 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.
- 1CodeStory studies your level, from raw beginner to placement ready.
- 2CodeStory describes online and campus batches, with timings that suit your plans.
- 3You decide. No pressure, no payment on the call.