The 60-second answer
Maintain a deque and running sum for the active window. On each value, append and add; if the window exceeds k, remove the oldest value and subtract it.
Build the answer in this order
1
Clarify constraints
Maintain a deque and running sum for the active window.
2
Choose the approach
On each value, append and add; if the window exceeds k, remove the oldest value and subtract it.
3
Prove complexity
Return running_sum / current_window_length; each update is O(1) time with O(k) storage.
4
Test edge cases
Handle invalid k, numeric precision, and small/partially-filled windows explicitly.
A useful interview mental model
This is the shape of a strong answer—not a script to memorize.
01Clarify
02Approach
03Implement
04Test
05Complexity
Senior-level signal
- Discuss concurrency semantics if the stream is consumed by multiple workers.
- At extreme scale, explain when approximate or sketch-based aggregation is preferable.
What the interviewer is really testing
Problem decomposition, correctness, data-structure choice, complexity reasoning, and clean implementation under pressure.
Likely follow-up questions
Can you improve the time or space complexity?
Which edge case is most likely to break this solution?
How would you test this under interview time pressure?
Common weak-answer patterns
- Starting to code before constraints are clear.
- Giving complexity without explaining why it is correct.
- Skipping adversarial and boundary cases.