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.
Implement a class that maintains a time-based sliding window of records and can return the average score within the current window.
window_size
(a duration in the same units as
timestamp
, e.g., seconds).
(timestamp, score)
.
insert_record(timestamp, score)
: insert a new record.
get_average(current_time)
: return the average of all
score
values whose
timestamp
is within the current window.
At any operation time T (i.e., during insert_record or get_average), records are considered valid if:
timestamp
is in the range
[T - window_size, T]
(inclusive), and
T - window_size
must be evicted before proceeding.
get_average
should return a sensible value when there are no valid records (define behavior, e.g., return
0
or
null
).
Describe/implement the class and its methods with appropriate time and space complexity considerations.