I jumped straight to a deque sorted by timestamp, which was the right call, but I fumbled explaining the eviction logic.
Start by clarifying requirements: what does 'rolling average over a configurable time window' mean? Is the window based on time (e.g., last 5 minutes) or count (e.g., last N scores)? Then propose a data structure that supports efficient insertion and eviction, such as a queue (for count-based) or a deque with timestamps (for time-based), and analyze the time and space complexity of each operation. Finally, discuss trade-offs and potential optimizations.
Pro tip: Mention that for a time-based window, you can use a deque of (timestamp, score) pairs and maintain a running sum to achieve O(1) amortized time per operation, but be prepared to discuss edge cases like out-of-order timestamps or clock skew.
Ask whether the window is time-based or count-based, whether scores can be added out of order, and what the expected throughput is. This determines the appropriate data structure.
For count-based, a queue (or circular buffer) works; for time-based, a deque of (timestamp, score) pairs. Explain why this structure supports efficient eviction and average calculation.
Describe addScore(score, timestamp) and getAverage(). For addScore, append and update sum, then evict expired entries. For getAverage, return sum / count if count > 0 else 0.
State time complexity: O(1) amortized for addScore (each element enqueued/dequeued once) and O(1) for getAverage. Space complexity: O(W) where W is the maximum number of scores in the window.
Mention alternatives like a balanced BST for range queries, or a ring buffer for fixed-size windows. Discuss handling out-of-order timestamps, concurrency, and precision issues.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.