Daily iOS
CS fundamentals and algorithms

Streaming moving average

Frame times arrive as a stream, one per frame. Design a type that returns "the average of the last k frames" instantly whenever asked. It's called 60 times a second, so the math has to stay light. Explain the data structure, how you update it, and the complexity.

How would an interviewer hear this answer? You'll see whether they'd move on, dig in, or say no, and which sentences would trip you up. Your score doesn't change.
See the short answer

Keep a ring buffer of size k and a running sum; when a new value arrives, subtract the old value it overwrites from the sum and add the new one, an O(1) update, and the average is the sum divided by the current count. Memory stays fixed at O(k).

Share a question