Repeated BST Range Sum and Average
Updated
Problem statement
Given a binary search tree with unique integer keys and an ordered batch of inclusive range queries, preprocess the tree and answer every query.
For each query [low, high], return one row [sum, average]:
sumis the sum of all keysxsatisfyinglow <= x <= high.averageis their arithmetic mean.- When the range contains no key, return
[0.0, 0.0].
Return the rows in query order. Both values use double in the runner output.
Function
rangeAggregates(root: TreeNode, queries: int[][]) → double[][]Examples
Example 1
root = [10,5,15,3,7,null,18]queries = [[7,15],[6,10],[16,17]]return = [[32.0,10.666666666666666],[17.0,8.5],[0.0,0.0]]The first range contains 7, 10, and 15. The second contains 7 and 10. The third is empty.
Example 2
root = [4,2,6,1,3,5,7]queries = [[1,7],[4,4],[8,9]]return = [[28.0,4.0],[4.0,4.0],[0.0,0.0]]The full range contains all seven keys, the single-point range contains only 4, and the last range contains none.
Example 3
root = []queries = [[-5,5]]return = [[0.0,0.0]]An empty tree contributes no values to any range.
Constraints
- The tree contains from
0through200000nodes. - Every key is unique and lies between
-10^9and10^9, inclusive. - The input satisfies the binary search tree ordering invariant.
1 <= queries.length <= 200000.- Every query satisfies
-10^9 <= low <= high <= 10^9. - Every range sum fits a signed 64-bit integer.
More Meta Onsite Interview questions
- Multi-Head Attention Forward PassPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Run-Length Encoded Vector Dot ProductPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Mutual-Friend RecommendationsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Deterministic K-Means AssignmentsPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- Linear Layer Forward and Backward PassesONSITE INTERVIEW · Seen Sep 2026
- Reservoir Sampling with Recorded DrawsONSITE INTERVIEW · Seen Sep 2026
- Stochastic Gradient Descent with MomentumONSITE INTERVIEW · Seen Sep 2026
- Top K Largest Values with a HeapONSITE INTERVIEW · Seen Sep 2026
- Partition a Card Deck into FifteensONSITE INTERVIEW · Seen Sep 2026
- Insert into a Sorted Circular ListONSITE INTERVIEW · Seen Aug 2026
Similar questions at other companies
- Totals from a Tiered Rate TableMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Random Pick with Weight Using Recorded DrawsGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
- Insert into a Left-Complete Binary TreeRubrik · ONSITE INTERVIEWRubrik interview questions
- Maintain a Consistent-Hashing RingCohesity · ONSITE INTERVIEW
- Kth Closest Element in a BSTLinkedIn · ONSITE INTERVIEWLinkedIn interview questions
- Farthest Affordable Shop per QueryAdobe · ONSITE INTERVIEWAdobe interview questions
- Random Pick with Weight Using Recorded DrawsAmazon · ONSITE INTERVIEW
- Lowest Common Ancestor in a BSTPeopleStrong · ONSITE INTERVIEWPeopleStrong interview questions
- Multinomial Sampling with Distribution ChecksNVIDIA · ONSITE INTERVIEWNVIDIA interview questions
- Delete Node in a BSTBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions