Rolling Average of the Last N Request Latencies
Last updated: November 29, 2026
Quick Overview
Implement a class that receives request latency samples one at a time and, after each sample, returns the average of the most recent N samples (or of all samples if fewer than N have arrived).
Meta
November 29, 20260
0
0 solved
Implement a class that receives request latency samples one at a time and, after each sample, returns the average of the most recent N samples (or of all samples if fewer than N have arrived).
A quick opener in Meta phone screens, framed around the latency dashboards that service owners watch. The interviewer checks that you keep a running sum rather than re-summing the window, and handles the warm-up period correctly.
What the Interviewer Expects
- Keep a running total instead of summing each time
- Use a deque of size N for the window
- Divide by the current window length during warm-up
- Validate the window size
- Mention floating point drift for very long streams
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- How would you report the p99 over the window instead of the mean?
- How would you make this a time-based window (last 60 seconds)?
- How would you avoid accumulated floating point error over billions of samples?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
add(x)appends a sample and returns the current average.- Window size N is fixed at construction and positive.
- Samples are numbers; billions may arrive over the life of the object....
High-Level Design
Maintain a deque of the latest samples and their sum. Each add appends the new sample and adds it to the sum. If the deque grows past N, pop the oldest from the left and subtract it. The answer is `...