Design a sliding-window moving average that excludes the k largest values
Quick Overview
Design a class that returns the moving average of an integer stream over a fixed-size sliding window, leaving out the k largest values currently in the window. It tests how you track both arrival order and value order, keep each call fast for large windows, and handle duplicates and a window that is still filling.
Design a sliding-window moving average that excludes the k largest values
Company: Google
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: hard
Interview Round: Other
Design a data structure that calculates the moving average of a stream of integers within a sliding window that holds the last `size` values, while **excluding the k largest values** currently in the window.
When a new number is added and the window already contains `size` elements, the oldest element is removed first. The average is then computed over the remaining `(current window size - k)` elements after removing the `k` largest values from consideration.
If the number of elements currently in the window is less than or equal to `k`, return `0.0`, since there are no elements left to average.
Implement the following methods:
- `MovingAverageWithoutTopK(size: int, k: int)` creates an empty window that holds at most `size` values and leaves out the `k` largest.
- `next(val: int) -> float` adds a new integer `val` to the sliding window. If the window exceeds its capacity, the oldest element is removed. It returns the average of the window elements excluding the `k` largest values, or `0.0` if the window has `k` or fewer elements.
If several values tie for the largest positions, it does not matter which copies are excluded, because the excluded sum is the same.
Example with `size = 4` and `k = 2`:
```text
Input:
["MovingAverageWithoutTopK", "next", "next", "next", "next"]
[[4, 2], [5], [3], [8], [6]]
Output: [null, 0.0, 0.0, 3.0, 4.0]
```
The first two calls leave 1 and then 2 values in the window, which is not more than `k = 2`, so both return `0.0`. After `next(8)` the window is `[5, 3, 8]`. Excluding 8 and 5 leaves `[3]`, so the average is 3.0. After `next(6)` the window is `[5, 3, 8, 6]`. Excluding 8 and 6 leaves `[5, 3]`, so the average is 4.0.
Example with `size = 3` and `k = 1`, where the window fills and starts evicting:
```text
Input:
["MovingAverageWithoutTopK", "next", "next", "next", "next", "next"]
[[3, 1], [4], [7], [1], [5], [2]]
Output: [null, 0.0, 4.0, 2.5, 3.0, 1.5]
```
`next(5)` evicts 4 and leaves `[7, 1, 5]`. Without 7, the average of 1 and 5 is 3.0. `next(2)` evicts 7, which was the excluded value, and leaves `[1, 5, 2]`. Now 5 is excluded and the average of 1 and 2 is 1.5.
```hint Two orders at once
Eviction needs the values in arrival order, while finding the k largest needs them in value order. Think about keeping both views of the same window in sync.
```
```hint Cost per call
Summing the k largest values on every call costs O(k). Ask which values change roles when one value arrives and one leaves, and what you could keep updated so a call touches only those.
```
```hint Duplicates
The same value can appear many times in the window. Check that an eviction removes exactly one copy, and that your container does not merge equal values into one.
```
### Constraints & Assumptions
- `1 <= size <= 10^4`
- `0 <= k < size`
- `-10^5 <= val <= 10^5`
- At most `10^5` calls are made to `next`.
### Clarifying Questions to Ask
- Is O(size) work per call acceptable at these limits, or should each call run in about O(log size)?
- Can I use a sorted container library, or only the standard library?
- Do `size` and `k` stay fixed for the life of the object?
### What a Strong Answer Covers
- How the arrival order needed for eviction is tracked alongside the value order needed to find the k largest.
- How the sum of the averaged values is kept current without rescanning the window on each call.
- How duplicate values are handled on insertion and eviction.
- Warm-up behavior: the `0.0` case and the divisor while the window is still filling.
- Time and space per call, comparing a simple baseline with a faster design.
### Follow-up Questions
- How would you change the design to exclude both the k largest and the k smallest values?
- How would the design change if the window covered the last T seconds instead of the last `size` values?
- If `k` could change between calls, how would you rebalance the structure, and what would that cost?
- How would you get the same bounds using only `heapq`, without a sorted container library?
Overview: Design a class that returns the moving average of an integer stream over a fixed-size sliding window, leaving out the k largest values currently in the window. It tests how you track both arrival order and value order, keep each call fast for large windows, and handle duplicates and a window that is still filling.
Design a data structure that calculates the moving average of a stream of integers within a sliding window that holds the last size values, while excluding the k largest values currently in the window.
When a new number is added and the window already contains size elements, the oldest element is removed first. The average is then computed over the remaining (current window size - k) elements after removing the k largest values from consideration.
If the number of elements currently in the window is less than or equal to k, return 0.0, since there are no elements left to average.
Implement the following methods:
MovingAverageWithoutTopK(size: int, k: int)
creates an empty window that holds at most
size
values and leaves out the
k
largest.
next(val: int) -> float
adds a new integer
val
to the sliding window. If the window exceeds its capacity, the oldest element is removed. It returns the average of the window elements excluding the
k
largest values, or
0.0
if the window has
k
or fewer elements.
If several values tie for the largest positions, it does not matter which copies are excluded, because the excluded sum is the same.
The first two calls leave 1 and then 2 values in the window, which is not more than k = 2, so both return 0.0. After next(8) the window is [5, 3, 8]. Excluding 8 and 5 leaves [3], so the average is 3.0. After next(6) the window is [5, 3, 8, 6]. Excluding 8 and 6 leaves [5, 3], so the average is 4.0.
Example with size = 3 and k = 1, where the window fills and starts evicting:
next(5) evicts 4 and leaves [7, 1, 5]. Without 7, the average of 1 and 5 is 3.0. next(2) evicts 7, which was the excluded value, and leaves [1, 5, 2]. Now 5 is excluded and the average of 1 and 2 is 1.5.
Constraints & Assumptions
1 <= size <= 10^4
0 <= k < size
-10^5 <= val <= 10^5
At most
10^5
calls are made to
next
.
Clarifying Questions to Ask Guidance
Is O(size) work per call acceptable at these limits, or should each call run in about O(log size)?
Can I use a sorted container library, or only the standard library?
Do
size
and
k
stay fixed for the life of the object?
What a Strong Answer Covers Guidance
How the arrival order needed for eviction is tracked alongside the value order needed to find the k largest.
How the sum of the averaged values is kept current without rescanning the window on each call.
How duplicate values are handled on insertion and eviction.
Warm-up behavior: the
0.0
case and the divisor while the window is still filling.
Time and space per call, comparing a simple baseline with a faster design.
Follow-up Questions Guidance
How would you change the design to exclude both the k largest and the k smallest values?
How would the design change if the window covered the last T seconds instead of the last
size
values?
If
k
could change between calls, how would you rebalance the structure, and what would that cost?
How would you get the same bounds using only
heapq
, without a sorted container library?