Count Pricing Zones Whose Fare Equals Subtree Average

Last updated: November 29, 2026

Quick Overview

A binary tree models nested pricing zones, each holding an integer base fare. Count the nodes whose value equals the floor of the average of all values in that node's subtree, including itself.

Uber
Coding & Algorithms
Software Engineer
Uber
November 29, 2026
Software Engineer
Technical Screen
Coding & Algorithms
Medium

0

0

0 solved


A binary tree models nested pricing zones, each holding an integer base fare. Count the nodes whose value equals the floor of the average of all values in that node's subtree, including itself.

An Uber technical screen problem about aggregating information bottom-up. The interviewer checks that you return multiple values from a recursive helper instead of recomputing subtree sums repeatedly.

What the Interviewer Expects
  • Returns (sum, count) from each subtree in one post-order pass
  • Avoids the O(n^2) approach of recomputing sums per node
  • Uses integer floor division correctly
  • Explains why a leaf always counts
  • States O(n) time and O(h) space
Key Topics to Cover
binary tree
post-order traversal
subtree aggregation
recursion
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • What if you needed the zone with the maximum subtree average instead?
  • How would you support updates to a single fare and re-answer quickly?
  • What changes if values can be negative and floor semantics matter?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements
  • Input: root of a binary tree with non-negative integer fares.
  • Output: number of nodes where node.val == floor(sum(subtree) / size(subtree)).
  • Every leaf qualifies since its subtree is itself....
High-Level Design

A naive solution computes the subtree sum and size for every node separately, which is O(n2)O(n^2) on a skewed tree. Instead, a post-order helper returns (total, size) for its subtree. The parent combi...


Submit Your Answer
Markdown supported