The core question wasn't that hard but the part that tripped me up was when they asked me to state my time semantics upfront.
Start by clarifying requirements: the window is relative to the latest timestamp, and we need average score of events within that window. Then propose a data structure like a deque (or balanced BST) to maintain events in the window, and discuss how to efficiently compute the average. Finally, analyze time and space complexity and consider edge cases.
Pro tip: Emphasize that the window is dynamic and based on the latest timestamp, so you must evict old events as new ones arrive. Mention that maintaining a running sum allows O(1) average retrieval, but eviction may require O(1) amortized time with a deque.
Ask about the definition of 'active window': is it inclusive? What if timestamps are out of order? What should be returned if no events are in the window? Confirm that the window slides based on the latest timestamp seen.
Propose using a deque (double-ended queue) to store events in timestamp order, along with a running sum of scores. Alternatively, consider a balanced BST if timestamps are not monotonic, but note that the problem implies events arrive in order.
When recording an event, append it to the deque, add its score to the running sum, and then remove all events from the front whose timestamp is outside the window relative to the new latest timestamp. Update the sum accordingly.
Return the running sum divided by the number of events in the deque. Handle the case of an empty deque by returning 0 or throwing an exception as appropriate.
Discuss time complexity: O(1) amortized per record (each event added and removed once), O(1) for getAverage. Space O(W) where W is max events in window. Mention edge cases: out-of-order timestamps, duplicate timestamps, window length zero, and large window.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.