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
Coding & Algorithms
Software Engineer
Meta
November 29, 2026
Software Engineer
Phone Screen
Coding & Algorithms
Easy

0

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
Queue
Sliding window
Running sum
Streaming
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. 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 Problems
Sample 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 `...


Submit Your Answer
Markdown supported