The median-of-a-stream part I knew cold, two heaps, keep them balanced, done.
Use two heaps (max-heap for lower half, min-heap for upper half) to maintain the median, combined with a queue or circular buffer to track the sliding window of the most recent N reviews. When a new review arrives, evict the oldest if the window is full, then insert the new review into the appropriate heap and rebalance to keep the heaps' sizes within one. This yields O(log N) per update and O(1) median retrieval.
Pro tip: Mention that lazy deletion can handle evictions efficiently: mark the evicted review as invalid and only remove it from the heap when it reaches the top, avoiding O(N) removal. Also, clarify how you handle duplicates and the exact median definition (average of two middle values for even N).
Confirm the definition of median (average of two middle values for even N), the data type of ratings (e.g., integers 1-5), and whether N is fixed or can change. Discuss expected update frequency and memory constraints.
Use a queue (or circular buffer) to store the most recent N reviews in order. When a new review arrives and the window is full, dequeue the oldest review and mark it for removal from the heaps.
Keep a max-heap for the lower half and a min-heap for the upper half. Insert new reviews into the appropriate heap based on comparison with the current median, then rebalance so that the size difference is at most 1.
Use lazy deletion: maintain a count of invalid (evicted) elements in each heap. When the top of a heap is invalid, pop it and decrement the count. This avoids O(N) removal and keeps updates O(log N).
State that each insertion and eviction (amortized) is O(log N), and median retrieval is O(1). Discuss edge cases: empty window, window not yet full, all equal ratings, and N=1.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.