FastPrepRepeated BST Range Sum and Average
Problems/Repeated BST Range Sum and Average

Repeated BST Range Sum and Average

Meta logoMeta● MediumFULLTIMEONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in a Meta onsite interview. The original report gives sums over a range in a BST, a follow-up for range averages, and speeding up repeated queries with prefix sums. It leaves out whether range ends count, what an empty range gives, and how to write a batch of queries. Here, queries are finite and include both ends. Our version is about 86% the same as the reported question.

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]:

  • sum is the sum of all keys x satisfying low <= x <= high.
  • average is 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]]
root
105371518

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]]
root
4213657

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]]
root
empty

An empty tree contributes no values to any range.

Constraints

  • The tree contains from 0 through 200000 nodes.
  • Every key is unique and lies between -10^9 and 10^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

Interview reports that mention this question

  • Meta Software Engineer Onsite Questions: 3 Experiences

Meta interview experiences

  • Meta PhD SWE Intern AI Coding Phone Screen: 1 Experience
  • Meta Business Support Engineer Phone Screen: 1 Experience
  • Meta AI Native Engineer OA and Onsite: 1 Experience
  • Meta AI-Native Coding Interview: 3 Experiences
  • Meta Production Engineer Intern Questions: 6 Experiences
All Meta interview questionsSee Meta hiring insights
Saved
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public double[][] rangeAggregates(TreeNode root, int[][] queries) {
        // Write your code here.
    }
}
Checking account…