Implement sliding-window timestamped average
BloombergSoftware Engineer · Technical ScreenLast updated
This question evaluates understanding of time-based sliding-window data structures, streaming aggregation, and the handling of timestamped records with efficient incremental updates and evictions.
Problem
Implement a class that maintains a time-based sliding window of records and can return the average score within the current window.
Requirements
- The class is initialized with an integer
window_size(a duration in the same units astimestamp, e.g., seconds). - Each record is a pair
(timestamp, score). - The class exposes two methods:
insert_record(timestamp, score): insert a new record.get_average(current_time): return the average of allscorevalues whosetimestampis within the current window.
Window definition
At any operation time T (i.e., during insert_record or get_average), records are considered valid if:
timestampis in the range[T - window_size, T](inclusive), and- any record older than
T - window_sizemust be evicted before proceeding.
Additional notes / assumptions
get_averageshould return a sensible value when there are no valid records (define behavior, e.g., return0ornull).- Timestamps may be non-decreasing across calls (state and use this assumption if needed), or you may design to handle out-of-order timestamps.
Deliverables
Describe/implement the class and its methods with appropriate time and space complexity considerations.
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.