Fixed Window Average and Mode
Updated
Problem statement
You keep a window that holds at most capacity numbers. It starts empty and supports three operations:
"add x": put the integerxinto the window. If the window now holds more thancapacitynumbers, remove the number that was added earliest."average": return the average of the numbers in the window."mode": return the value that appears most often in the window. If several values are tied for the most, return the smallest of them.
You are given capacity and a list operations. Run the operations in order on one window, and return a list with the result of every "average" and "mode" operation, in the order they appear. An "add x" operation adds nothing to this list.
The average is the exact mean as a decimal number, without rounding. The mode is returned as a decimal number too, for example 5.0. An answer is accepted if it is within a relative error of 10^-9 of the exact value (an absolute error of 10^-9 for values between -1 and 1).
Function
windowAverageAndMode(capacity: int, operations: String[]) → double[]Examples
Example 1
capacity = 3operations = ["add 1","add 2","add 2","mode","average","add 5","add 5","mode","average"]return = [2.0,1.6666666666666667,5.0,4.0]After three adds the window is [1, 2, 2]. The value 2 appears twice, so the mode is 2, and the average is 5 / 3. Adding 5 makes four numbers, so the earliest one, 1, leaves: [2, 2, 5]. Adding the second 5 pushes out the first 2: [2, 5, 5]. Now 5 appears twice, so the mode is 5, and the average is 12 / 3 = 4.
Example 2
capacity = 3operations = ["add 4","add 9","add 4","mode","add 9","mode","add 1","mode","average"]return = [4.0,9.0,1.0,4.666666666666667]The window [4, 9, 4] has mode 4. Adding 9 pushes out the first 4, leaving [9, 4, 9], so the mode becomes 9. Adding 1 pushes out the first 9, leaving [4, 9, 1]. Each value now appears once, so the smallest of them, 1, is the mode. The average is 14 / 3.
Example 3
capacity = 2operations = ["add -3","average","add -4","average","mode","add 10","average","mode"]return = [-3.0,-3.5,-4.0,3.0,-4.0]With one number the average is -3. After -4 the window is [-3, -4]: the average is -3.5, and both values appear once, so the smaller one, -4, is the mode. Adding 10 pushes out -3, leaving [-4, 10]: the average is 3 and the mode is still -4, the smaller of two values that each appear once.
Constraints
1 <= capacity <= 50001 <= operations.length <= 10^4- Each
operations[i]is"add x","average"or"mode". -10^6 <= x <= 10^6, written as a plain integer with no plus sign and no leading zeros.operations[0]is an"add x"operation.
More Nuro interview questions
- Merge Timestamped Record ListsPHONE SCREEN · Seen Oct 2026
- Assign Robots to Their Nearest Available ItemsPHONE SCREEN · Seen Oct 2026
- Container With Most WaterPHONE SCREEN · Seen Oct 2026
- Course Ordering with Concurrent UpdatesPHONE SCREEN · Seen Sep 2026
- Group Sparse Points by Distance ThresholdPHONE SCREEN · Seen Sep 2026
- Reveal a Minesweeper RegionPHONE SCREEN · Seen Aug 2026
- Schedule Buffered Video PlaybackPHONE SCREEN · Seen Aug 2026
- Maximum Value for Two Delivery RobotsPHONE SCREEN · Seen Jul 2026
- Thread-Safe Task Storage and Worker DispatchONSITE INTERVIEW · Seen Sep 2026
- Trapping Rain WaterONSITE INTERVIEW · Seen Sep 2026
Similar questions at other companies
- Rate Limiter with a Sliding Window per ClientStackAdapt · PHONE SCREENStackAdapt interview questions
- Per-Client API Rate LimiterDocker · PHONE SCREEN
- Per-Client Sliding-Window Rate LimiterCVS Health · PHONE SCREEN
- Sliding-Window Rate LimiterUber · PHONE SCREENUber interview questions
- Recent Advertisement Click CountsAmazon · PHONE SCREEN
- Delayed Streaming Duplicate SuppressionGoogle · PHONE SCREENGoogle interview questions
- Completions API Rate LimiterCursor · PHONE SCREENCursor interview questions
- Sliding-Window Rate LimiterStripe · PHONE SCREEN · ONSITE INTERVIEWStripe interview questions
- 15-Minute Chat Session TrackerOpenAI · PHONE SCREEN · ONSITE INTERVIEWOpenAI interview questions
- Key-Value Store GET and PUT QPSDatabricks · PHONE SCREENDatabricks interview questions