Average Values by Binary-Tree Level

Reported by candidates from Motive's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Motive OA. Under 2s to a working solution.
Founder's read

The mistake that sinks a first attempt on this Motive question is integer overflow, and it's the one thing nobody warns you about. Motive's OA, reported in May 2024, asks for the average value at each depth of a binary tree. It's a level-order traversal, plain BFS, and the logic takes five minutes. But node values go up to a billion and the tree can hold 100000 nodes, so a 32-bit sum will wrap and quietly hand you wrong answers. If your mind goes blank mid-assessment, StealthCoder runs invisibly as a safety net and gives you the working solution.

The problem

Given the root of a non-empty binary tree, return an array whose value at index d is the arithmetic mean of all node values at depth d.
The root is at depth zero. Return averages from the root level down to the deepest level.

Function
averageOfLevels(root: TreeNode) → double[]

Examples
Example 1
root = [3,9,20,null,null,15,7]
return = [3.0,14.5,11.0]
The three levels are [3], [9,20], and [15,7].
Example 2
root = [5]
return = [5.0]
A one-node tree has one level.

Constraints
The tree contains 1 through 100000 nodes.
-1000000000 <= node.val <= 1000000000.
Answers within 1e-5 of the exact average are accepted.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The pattern is breadth-first search by level. Put the root in a queue. For each round, record the current queue size, pop exactly that many nodes, add their values to a running sum, and push their children. Divide the sum by the count and append it to the result. The pitfall is the sum type. Values reach 1e9 and a level can hold tens of thousands of nodes, so the total blows past 2^31. Use a long or a double for the accumulator, never an int. The second trap is integer division. Convert to double before you divide, or 14.5 turns into 14. A recursive DFS works too, tracking sums and counts per depth in arrays, but BFS is simpler to get right. Runtime is O(n) and extra space is O(width). If you freeze during the live OA, StealthCoder is the hedge that surfaces this exact template.

If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.

If this hits your live OA

You can drill Average Values by Binary-Tree Level cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it.

Get StealthCoder

Related leaked OAs

⏵ The honest play

You've seen the question. Make sure you actually pass Motive's OA.

Motive reuses patterns across OAs. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Average Values by Binary-Tree Level FAQ

How hard is the Motive average of levels question really?+

Easy. It's a textbook level-order traversal with a sum and a count per level. The difficulty is in the details, mainly overflow and integer division. If you've written BFS on a tree before, you'll finish it fast.

What's the trick to getting it right the first time?+

Snapshot the queue size at the start of each level, then loop exactly that many times. That cleanly separates levels. Accumulate in a 64-bit type or double, and divide as a double so you keep the fractional part.

Why does overflow matter here?+

Node values reach 1e9 in magnitude and a single level can have tens of thousands of nodes. The sum can exceed what a 32-bit int holds, which silently gives wrong averages. A long or double accumulator avoids it completely.

Can I solve it with DFS instead of BFS?+

Yes. Pass the depth down the recursion, and keep a list of sums and a list of counts indexed by depth. Divide at the end. It works, but with 100000 nodes a skewed tree can cause deep recursion, so BFS is the safer choice.

How do I prepare in 48 hours for this kind of question?+

Write BFS level-order on a tree from memory twice, once collecting lists and once computing averages. Then check edge cases: single node, skewed tree, negative values, and large values. That covers most tree-level OA questions.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Motive.

OA at Motive?
Invisible during screen share
Get it