MEDIUMasked at 1 company

Minimum Time to Collect All Apples in a Tree

A medium-tier problem at 63% community acceptance, tagged with Hash Table, Tree, Depth-First Search. Reported in interviews at Myntra and 0 others.

Founder's read

You've got a tree, some apples scattered across nodes, and a single trip to collect them all starting from node zero. You have to return to the root when done. This problem appears on Myntra assessments and hammers on a specific DFS trick that most candidates miss on first attempt: you don't traverse every edge, only the ones that lead to apples or to subtrees containing apples. The acceptance rate sits at 63%, which means a third of people either TLE or return wrong counts because they explore dead branches. If this hits your live OA and you blank on the pruning logic, StealthCoder solves it invisibly.

Companies asking
1
Difficulty
MEDIUM
Acceptance
63%

Companies that ask "Minimum Time to Collect All Apples in a Tree"

If this hits your live OA

Minimum Time to Collect All Apples in a Tree is the kind of problem that decides whether you pass. StealthCoder reads the problem on screen and surfaces a working solution in under 2 seconds. Invisible to screen share. The proctor sees nothing. Built because the OA filter rejects engineers who'd pass the on-site. That's a broken filter. This is the workaround.

Get StealthCoder
What this means

The naive approach visits every node and counts 2 times the cost of every edge you traverse. It fails because you end up walking paths that have zero apples. The real pattern: use DFS to return a boolean indicating whether a subtree contains any apples (either at that node or deeper). Only traverse an edge if the child's subtree returned true or the child itself is an apple node. You accumulate 2 times the edge cost only for edges you actually visit. Common pitfall is forgetting you must return to the root, so every edge costs twice. Another trap: computing time for all edges first, then filtering. The trick is filtering during traversal itself. Myntra's version tests whether you can recognize that pruned DFS outperforms blind tree traversal. StealthCoder handles the state-management logic in seconds if the pattern doesn't click during your attempt.

Pattern tags

The honest play

You know the problem. Make sure you actually pass it.

Minimum Time to Collect All Apples in a Tree recycles across companies for a reason. It's medium-tier, and most candidates blank under the timer. StealthCoder is the hedge: an AI overlay invisible during screen share. It reads the problem and surfaces a working solution in under 2 seconds. Built because the OA filter rejects engineers who'd pass the on-site. That's a broken filter. This is the workaround. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Minimum Time to Collect All Apples in a Tree interview FAQ

Is this really just a tree traversal problem?+

No. It's a tree traversal where you prune aggressively. You must recognize that edges leading to subtrees with no apples waste time. DFS is the right tool, but the key insight is filtering edges before you count them, not after.

Why is the acceptance rate only 63% if this is medium difficulty?+

Because most people either count all edges (wrong) or they TLE by exploring dead branches. The pruning logic isn't intuitive on a timed assessment. Once you see that you only traverse edges that lead to apples, it clicks, but the first attempt usually misses it.

Do I need BFS, or is DFS the only way?+

DFS is the standard approach and much cleaner for this problem. You need to know the entire subtree state (whether it contains apples) before deciding whether to count the edge. BFS doesn't give you that parent-child visibility as naturally.

What's the most common wrong answer on this problem?+

Counting every edge twice without checking if the subtree actually has apples. This returns a time that's way too high. Another common mistake is forgetting the factor of 2, since you must return to root after collecting from any branch.

How does this relate to the Tree topic more broadly?+

This problem combines basic tree traversal (Hash Table for adjacency, DFS for search) with a critical optimization: recognizing which parts of the tree are worth visiting. It's a stepping stone to more complex tree DP problems.

Want the actual problem statement? View "Minimum Time to Collect All Apples in a Tree" on LeetCode →

Frequency and company-tag data sourced from public community-maintained interview-report repos. Problem, description, and trademark © LeetCode. StealthCoder is not affiliated with LeetCode.