Stream Maximum, Mean and Mode
Updated
Problem statement
Maintain a collection of inserted integers and answer three queries: its maximum, arithmetic mean and mode. The mode is the value with the highest frequency; if values tie, return the smallest tied value.
The input operations represents calls to the class. Every operation has two integers:
[0, value]: insert one occurrence ofvalue.[1, 0]: query the maximum.[2, 0]: query the mean.[3, 0]: query the mode.
Return the result of every query in its original order as a number in a double[]. Insertion produces no output. A query comes only after at least one insertion. Keep every inserted value: there is no time window and nothing is dropped. If there are no queries, return an empty array.
Aim for expected O(1) time per insertion and O(1) time per query. To do this, update the statistics as each value arrives. Space may grow with the number of distinct values.
Discussion follow-up: add a deletion method while keeping the three queries O(1). Discuss which information must change and what deletion cost is possible. This follow-up is discussed in the editorial; deletion is not an operation in the input.
Function
streamStats(operations: int[][]) → double[]Examples
Example 1
operations = [[0,4],[0,2],[0,4],[1,0],[2,0],[3,0]]return = [4,3.3333333333333335,4]The collection is [4,2,4]. Its maximum is 4, its mean is 10/3, and its mode is 4 because that value appears twice.
Example 2
operations = [[0,5],[0,1],[3,0],[0,5],[3,0],[2,0]]return = [1,5,3.6666666666666665]After 5 and 1 are inserted, both occur once, so the tied mode is the smaller value 1. Inserting another 5 makes 5 the mode. The final mean is 11/3.
Example 3
operations = []return = []There are no operations and therefore no query results.
Constraints
0 <= operations.length <= 100000- Every operation has exactly two integers. Its first integer is an operation code from 0 through 3.
-1000000 <= operations[i][j] <= 1000000- The second integer is 0 for every query.
- Every query follows at least one insertion.
- Means are checked with the usual floating-point tolerance.
More Akuna Capital Phone Screen questions
- Match Orders by Price and Arrival TimeOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Biased Coin: Probability of Exactly h HeadsPHONE SCREEN · Seen Nov 2026
- Positive Nth Root with Gradient DescentPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Two SumOA · PHONE SCREEN · Seen Sep 2026
- Maximum Value in a Binary TreePHONE SCREEN · Seen Sep 2026
- Tiered Expiring Item StorePHONE SCREEN · Seen Aug 2026
- Statistics for the Latest K ValuesPHONE SCREEN · Seen Nov 2025
- Remove Duplicates While Preserving OrderPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2025
- Sliding Window MaximumPHONE SCREEN · Seen Oct 2025
- Evaluate a PolynomialPHONE SCREEN · Seen Oct 2025
Similar questions at other companies
- Insert Delete GetRandom O(1)Charta Health · PHONE SCREEN
- Filter, Sort, and Deduplicate Scheduled TasksRippling · PHONE SCREEN · ONSITE INTERVIEW
- Longest Consecutive SequenceKotak Mahindra Bank · PHONE SCREEN
- Longest Consecutive SequenceJP Morgan Chase · PHONE SCREEN
- Linked Merchants Within Two HopsStripe · PHONE SCREEN
- Logger Rate Limiter with Every-Arrival HistoryGoogle · PHONE SCREEN
- Propagating Fraud DetectorAffirm · PHONE SCREEN · ONSITE INTERVIEW
- Top K Frequent Elements with a Recency TiebreakSwiggy · PHONE SCREEN
- Order and Batch Open-Close LifecycleHudson River Trading · PHONE SCREEN
- Pins Violation Log QueriesPinterest · PHONE SCREEN