Moving Average of the Last N Heartbeat Latencies
Last updated: November 29, 2026
Quick Overview
Azure Monitor style agents send heartbeat latencies as a stream. Implement a class whose `add(latency)` call returns the average of the most recent N latencies, or of all seen so far if fewer than N have arrived.
Microsoft
November 29, 20260
0
0 solved
Azure Monitor style agents send heartbeat latencies as a stream. Implement a class whose `add(latency)` call returns the average of the most recent N latencies, or of all seen so far if fewer than N have arrived.
This is a typical Microsoft phone screen opener on streaming data. The interviewer wants O(1) updates with a running sum and a bounded queue, then asks how it changes for time based windows.
What the Interviewer Expects
- Keep a bounded deque and a running total
- Update in O(1) per call without re-summing
- Handle the warm-up period before N values arrive
- Discuss floating point accumulation
- Extend to a time based window on request
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 compute the average over the last 60 seconds instead of last N values?
- How would you track a moving p95 instead of the mean?
- How do you avoid floating point drift over billions of updates?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
- Constructor takes window size N >= 1.
add(x)stores x and returns the mean of the last min(N, count) values.- Calls may number in the millions, so each must be O(1)....
High-Level Design
Hold the window in a deque and keep total equal to the sum of the deque. On each add, append the value and add it to total. If the deque grows past N, pop the oldest value from the left and subtra...