This question evaluates a candidate's understanding of streaming algorithms, sliding-window statistics, and resource-constrained data structure design for computing moving and weighted averages.
You are processing a stream of numeric events (e.g., user activity counts). You are given a fixed window size X.
Design a data structure / function that supports:
next(value) -> avg
where avg is the average of the most recent X values seen so far (or fewer than X if fewer values have arrived).
Modify the approach to compute a weighted moving average over the most recent X values where more recent values have higher weight.
For example, for the last k <= X values (oldest to newest):
1, 2, ..., k
Do this without storing all previous data points (i.e., memory should not grow with the total stream length).