Streaming Median and Kth Smallest
Updated
Problem statement
Process an ordered stream of integer updates and order-statistic queries. The value range is deliberately small, so every inserted value is between -1000 and 1000.
Each row in operations is one of:
["add", value]: insert one occurrence ofvalue.["median"]: return the current median. For an even number of values, use the average of the two middle values.["kth", k]: return the current one-indexedkth-smallest value.
Return one string for each query, in query order. Write integer results without a decimal point and half-integer medians with exactly one decimal digit, such as "2.5" or "-0.5". Add operations do not produce output.
Function
processOrderStatistics(operations: String[][]) → String[]Examples
Example 1
operations = [["add","1"],["add","5"],["median"],["add","2"],["median"],["kth","2"]]return = ["3","2","2"]The first median averages 1 and 5. After adding 2, both the median and the second-smallest value are 2.
Example 2
operations = [["add","-2"],["add","1"],["median"],["add","1"],["kth","3"],["median"]]return = ["-0.5","1","1"]The even median is (-2 + 1) / 2 = -0.5. Duplicate values count as separate stream elements.
Example 3
operations = [["add","1000"],["median"],["add","-1000"],["kth","1"],["kth","2"],["median"]]return = ["1000","-1000","1000","0"]The range endpoints are retained exactly, and their even median is 0.
Constraints
1 <= operations.length <= 100000- Every value is an integer in
[-1000, 1000]. - At least one value has been added before every query.
- For each kth query,
1 <= k <= current stream size.
More Google Onsite Interview questions
- Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Distinct Subset SumsONSITE INTERVIEW · Seen Nov 2026
- Playlist With No Same Artist in a RowONSITE INTERVIEW · Seen Nov 2026
- Delete Nodes and Return Forest RootsONSITE INTERVIEW · Seen Nov 2026
- Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- First and Last Target Position in a Mountain ArrayONSITE INTERVIEW · Seen Nov 2026
- Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- 0/1 Knapsack with Weight and Volume LimitsONSITE INTERVIEW · Seen Nov 2026
- Batched Subsequence Word MatchesONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Colored Rooted TreeRubrik · ONSITE INTERVIEW
- Longest Increasing Subsequence With Bounded Adjacent DifferenceAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Dynamic Fixed-K Score LeaderboardCoinbase · ONSITE INTERVIEW
- Streaming Interval Peak ConcurrencyUber · ONSITE INTERVIEW
- Range Sum Query in a Mutable GridBloomberg LP · ONSITE INTERVIEW
- Closest Pair Among the Latest K ReadingsRippling · ONSITE INTERVIEW
- Volunteer Awards: Totals Divisible by KNutanix · ONSITE INTERVIEW
- Perfect AND Tree Leaf UpdatesPure Storage · ONSITE INTERVIEW
- Maximum Observation in a Time WindowAkuna Capital · PHONE SCREEN · ONSITE INTERVIEW
- Count Pairs with K Divisible Values in the IntervalChubb · OA