← Amazon Interview Insights

Amazon·Software Engineer·Technical Phone Screen·Senior

SeniorPrefer not to say
Apr 2026

Summary

Amazon SWE interview with a pretty gnarly algorithmic problem centered on sliding window medians. The problem had enough moving parts that it took me a while to even see the full shape of it.

Questions Asked (1)

Q1

Given a continuous stream of product reviews, design a data structure that efficiently returns the median rating of the most recent N reviews at any point in time. When a new review comes in and the window is full, the oldest review should be evicted. Target O(log N) per update.

Algorithms & Data StructuresSystem DesignTechnical Trade-offs
Author's notes

The median-of-a-stream part I knew cold, two heaps, keep them balanced, done.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

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).

1. Clarify requirements and constraints

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.

2. Design the sliding window mechanism

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.

3. Maintain median with two 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.

4. Handle evictions efficiently

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).

5. Analyze complexity and edge cases

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.

Key Points to Mention

  • Two-heap approach for dynamic median maintenance
  • Sliding window with queue/circular buffer and lazy deletion
  • Time complexity: O(log N) per update, O(1) for median
  • Space complexity: O(N) for the window and heaps
  • Handling duplicates and invalid elements in heaps
  • Edge cases: even/odd N, window not full, all same ratings

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.