Region Subtree With the Highest Average Order Value
Last updated: November 29, 2026
Quick Overview
Given a tree of market regions where each node records revenue and order count directly attributed to it, find the subtree whose total revenue divided by total orders is highest.
DoorDash
November 29, 20260
0
0 solved
Given a tree of market regions where each node records revenue and order count directly attributed to it, find the subtree whose total revenue divided by total orders is highest.
DoorDash reports metrics up a geographic hierarchy (country, region, market, submarket), so a tree aggregation question is a natural fit. The interviewer is checking post-order traversal, returning aggregates instead of averages, and care with division by zero.
What the Interviewer Expects
- Use post-order DFS returning (revenue, orders) for each subtree
- Compute the average from sums, never by averaging child averages
- Skip subtrees with zero orders
- Track the best answer in a closure or return value
- Discuss recursion depth on deep trees
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- How would you require a minimum order count to avoid tiny noisy markets winning?
- How would you do this iteratively for a very deep tree?
- What if the tree is stored as parent pointers in a table?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
- Each node:
name,revenue(cents) andordersrecorded at that node, and children. - The subtree average is total revenue over total orders across the node and all descendants.
- Output: `(name,...
High-Level Design
Run a post-order DFS. Each call returns the subtree's summed revenue and order count. A parent adds its own values to its children's sums, then computes its own average. Averaging child averages is wr...