MEDIUMasked at 1 company

Diameter of N-Ary Tree

A medium-tier problem at 75% community acceptance, tagged with Tree, Depth-First Search. Reported in interviews at DoorDash and 0 others.

Founder's read

Diameter of N-Ary Tree is a medium-difficulty tree problem that DoorDash has asked. It shows up less frequently than binary tree variants, which is exactly why candidates blank on it during live assessments. The trick isn't complex, but the N-ary structure throws people off. You're finding the longest path between any two nodes in a tree where each node can have more than two children. Most candidates either overcomplicate it with graph theory or underestimate how the recursion changes when siblings can be many. If you haven't drilled this specific structure, StealthCoder solves it invisibly during your OA and you move on.

Companies asking
1
Difficulty
MEDIUM
Acceptance
75%

Companies that ask "Diameter of N-Ary Tree"

If this hits your live OA

Diameter of N-Ary 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. Made by a working FAANG engineer who treats the OA the way companies treat hiring: as a game with rules you should know.

Get StealthCoder
What this means

The core insight: diameter isn't the depth from the root. It's the longest path between any two leaves, which could pass through any node as a 'pivot'. For an N-ary tree, you compute the two tallest branches from each node and sum them. That sum is a candidate for the global diameter. The recursion is clean: DFS down, track the max depth from each subtree, update the diameter at every node as you bubble back up. The common mistake is thinking you only care about the root's two longest children, or coding it as if children were a fixed binary pair. With N children, you need to sort or iterate to find the first and second-longest branches. DFS with memoization or a single pass works. Acceptance rate is solid at 75 percent, but that's after people have seen N-ary trees before. If this is your first encounter in a live OA, the tree structure alone can eat ten minutes.

Pattern tags

The honest play

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

Diameter of N-Ary 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. Made by a working FAANG engineer who treats the OA the way companies treat hiring: as a game with rules you should know. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Diameter of N-Ary Tree interview FAQ

Is this harder than finding the diameter of a binary tree?+

Not algorithmically. The logic is the same: find the two deepest subtrees at each node and sum them. N-ary just means you iterate over children instead of accessing left and right. If you know the binary version, you can adapt in two minutes. The stumble is usually syntax and remembering to sort or track the top two, not the algorithm itself.

Do I need to do a second pass or can I solve it in one DFS?+

One pass is enough. Return the max depth of the subtree and update the global diameter at each node on the way back up. Track the diameter in an outer variable or pass it as a reference. Cleaner than two passes and shows you understand the recursion.

How do I find the two longest branches when a node has many children?+

Sort the depths of all children in descending order, grab the first two, and sum them. Or iterate once, tracking max and second-max. Sorting is clearer and doesn't hurt for reasonable tree sizes. No need to overcomplicate it.

Will DoorDash ask variations or just the basic diameter?+

Input data shows only DoorDash and only the basic problem. Variations exist, but you're likely seeing the core version: given an N-ary tree, return the longest path length. Master this, and any variant you see is a small tweak.

Why is the acceptance rate so high if people blank on it?+

Acceptance rate reflects people who've already prepped. You're reading this because you haven't. If this pops up in your live OA unprepared, that 75 percent won't help you. Studying it now closes that gap.

Want the actual problem statement? View "Diameter of N-Ary 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.