Statistics for the Latest K Values
Updated
Problem statement
Maintain the most recent k inserted integers. Once the collection is full, inserting a new value removes its oldest value. Before it is full, keep all values inserted so far.
For this exercise, values are integers from 1 through 1001. Support minimum, maximum, arithmetic mean, median and mode queries. The mode is the smallest value among those with the highest frequency. For an even-sized collection, the median is the average of its two middle sorted values.
Every row of operations has two integers:
[0, value]: insert one value.[1, 0]: query the minimum.[2, 0]: query the maximum.[3, 0]: query the mean.[4, 0]: query the median.[5, 0]: query the mode.
Return one number per query, in query order, in a double[]. Insertions produce no output. Every query follows at least one insertion. Return an empty array if there are no queries.
Keep every query O(1) by doing any necessary work during insertion. You may scan the fixed value range on insertion; state its O(U) cost with U = 1001 rather than claiming the same bound for an arbitrary value universe.
Function
windowStats(k: int, operations: int[][]) → double[]Examples
Example 1
k = 3operations = [[0,1],[0,3],[0,3],[2,0],[3,0],[5,0],[0,2],[1,0],[3,0],[4,0],[5,0]]return = [3,2.3333333333333335,3,2,2.6666666666666665,3,3]Initially the retained values are [1,3,3], so max=3, mean=7/3 and mode=3. Inserting 2 removes the oldest value 1, leaving [3,3,2]. Its minimum is 2, mean is 8/3, median is 3 and mode is 3.
Example 2
k = 2operations = [[0,9],[0,1],[4,0],[5,0],[0,4],[2,0],[4,0],[5,0]]return = [5,1,4,2.5,1]For [9,1], the sorted values are [1,9], so the median is 5 and the tied mode is 1. Inserting 4 removes 9. For the retained [1,4], max=4, median=2.5 and mode=1.
Example 3
k = 1operations = []return = []There are no operations and no query results.
Constraints
1 <= k <= 100000 <= operations.length <= 10000- Each operation has exactly two integers.
0 <= operations[i][j] <= 1001- Operation codes are from 0 through 5. An insertion value is from 1 through 1001; a query has second integer 0.
- Every query follows at least one insertion.
- Returned means are compared with the platform’s 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
- 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
- Reverse Nodes in Groups of KPHONE SCREEN · Seen Oct 2025
Similar questions at other companies
- Insert Delete GetRandom O(1)Charta Health · PHONE SCREENCharta Health interview questions
- Filter, Sort, and Deduplicate Scheduled TasksRippling · PHONE SCREEN · ONSITE INTERVIEWRippling interview questions
- Longest Consecutive SequenceKotak Mahindra Bank · PHONE SCREENKotak Mahindra Bank interview questions
- Longest Consecutive SequenceJP Morgan Chase · PHONE SCREENJP Morgan Chase interview questions
- Linked Merchants Within Two HopsStripe · PHONE SCREENStripe interview questions
- Logger Rate Limiter with Every-Arrival HistoryGoogle · PHONE SCREENGoogle interview questions
- Propagating Fraud DetectorAffirm · PHONE SCREEN · ONSITE INTERVIEWAffirm interview questions
- Top K Frequent Elements with a Recency TiebreakSwiggy · PHONE SCREEN
- Order and Batch Open-Close LifecycleHudson River Trading · PHONE SCREENHudson River Trading interview questions
- Pins Violation Log QueriesPinterest · PHONE SCREENPinterest interview questions
Akuna Capital interview experiences
- Akuna Capital Quant Trader and QR Interviews: 5 Experiences
- Akuna Capital JQR CodePair Rounds: 8 Experiences (Part 6 of 6)
- Akuna Capital QR Probability Phones: 14 Experiences (Part 5 of 6)
- Akuna Capital QR-ML Interviews: 11 Experiences (Part 4 of 6)
- Akuna Capital QR Regression Rounds: 11 Experiences (Part 3 of 6)
- Akuna Capital QR Coding Interviews: 17 Experiences (Part 2 of 6)