The basic record and get_average parts came pretty naturally.
Start by clarifying requirements: window size, operations, and performance constraints. Then propose a data structure combining a hash map for O(1) record/update and a balanced BST or order-statistic tree for efficient percentile queries, with lazy expiration or a time-indexed queue for sliding window eviction. Discuss trade-offs between exact and approximate methods, and outline the implementation with complexity analysis.
Pro tip: Mention that you would use a monotonic queue or a timestamped deque to efficiently expire old entries, and consider using a Fenwick tree for percentile queries if scores are bounded. This shows you balance simplicity and performance.
Ask about window size (fixed or sliding), score range, expected operations per second, and whether percentile needs to be exact. Confirm if updates can change timestamps.
Propose a hash map for O(1) record/update by ID, and a balanced BST (e.g., order-statistic tree) or Fenwick tree for percentile queries. For expiration, use a time-ordered queue or heap.
Describe lazy expiration: on each operation, remove entries older than the window from the queue and the BST. Alternatively, use a background thread or periodic cleanup.
Outline record: insert into map and BST, add to queue. Update: remove old score from BST, update map, insert new score. get_average: maintain running sum. get_percentile: query BST for k-th element.
Discuss time/space complexity: O(log n) for record/update/percentile, O(1) for average. Mention alternatives like approximate percentiles (t-digest) for high throughput, and concurrency considerations.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.