MEDIUMasked at 2 companies

Number of Good Leaf Nodes Pairs

A medium-tier problem at 72% community acceptance, tagged with Tree, Depth-First Search, Binary Tree. Reported in interviews at josh technology and 1 others.

Founder's read

You're staring at a tree problem on your OA. The problem asks you to count pairs of leaf nodes where the distance between them is exactly some threshold, usually under 10. It sounds straightforward until you realize you need to track paths from every leaf to every other leaf without blowing up your time complexity. ByteDance and Josh Technology have both asked this. The acceptance rate sits around 72 percent, which means most people who see it solve it, but the ones who don't usually hit a wall on the path-tracking logic. If you haven't drilled tree traversal with distance constraints, StealthCoder runs invisibly during your assessment and surfaces the working approach in seconds.

Companies asking
2
Difficulty
MEDIUM
Acceptance
72%

Companies that ask "Number of Good Leaf Nodes Pairs"

If this hits your live OA

Number of Good Leaf Nodes Pairs 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 trap is thinking you need to enumerate all leaf pairs explicitly. Instead, you do a single DFS pass where each node returns information about leaf paths below it. At every node, you combine the leaf distances from the left and right subtrees to count valid pairs where one leaf is on the left and one on the right. The trick is storing the distance from the current node to each leaf below it, then checking if the combined distance falls within your threshold. Common failure: trying to do multiple DFS runs per node, which explodes into O(n squared) or worse. The pattern uses Tree, Depth-First Search, and Binary Tree fundamentals. When you hit this live and your first instinct feels clunky, StealthCoder handles the distance-tracking boilerplate and the pair-counting merge logic so you don't waste 20 minutes on a medium.

Pattern tags

The honest play

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

Number of Good Leaf Nodes Pairs 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.

Number of Good Leaf Nodes Pairs interview FAQ

Is this problem actually asked at big companies like ByteDance?+

Yes. ByteDance and Josh Technology both have interview reports showing this exact problem. That said, it's a medium-tier ask, not the hardest tree problem they throw at candidates. It tests whether you understand path aggregation in trees, which is a real skill for systems-level work.

What's the actual trick to solving this in one pass?+

DFS returns a map of distance to leaf count for each subtree. At every node, you cross-multiply distances from left and right children to count valid pairs, then merge the two maps before returning up. This avoids re-traversing paths and keeps you at O(n log n) or O(n) depending on tree shape.

Why do so many people get stuck on this if the acceptance rate is 72 percent?+

Most people solve it, but the ones who don't usually fail on path reconstruction or distance calculation off-by-one errors. Tree problems punish sloppy traversal logic. If you're not confident in your DFS bookkeeping, you'll debug for 15 minutes on a detail that shouldn't matter.

Does this require tracking parent pointers or any special setup?+

No. The entire solution lives in recursive DFS. You don't need parent pointers or preprocessing. Some approaches rebuild the tree structure first, but a clean recursive solution with distance maps avoids that overhead and runs faster.

How does this differ from other tree distance problems I might see?+

Most tree distance problems ask for the distance between two specific nodes or the diameter. This one requires counting pairs across the whole tree that meet a constraint. It combines path tracking with aggregation, which is less common but shows up in real backend work.

Want the actual problem statement? View "Number of Good Leaf Nodes Pairs" 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.