← Amazon Interview Insights

Amazon·Software Engineer·Online Assessment (OA)·Intermediate

Intermediate
May 2026

Summary

Amazon SWE coding round with a streaming median problem. The window constraint made it interesting since you can't just sort on every query.

Questions Asked (1)

Q1

Design a comment rating system that supports two operations: adding a comment with an integer rating, and querying the median rating across the most recent N comments. The solution must handle streaming data efficiently without re-sorting on every median query.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

The naive approach of just grabbing the last N elements and sorting them every time is obviously too slow given the constraints.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Clarify the requirements first, especially the definition of 'most recent N comments' and whether N is fixed or variable. Then propose a solution using two heaps (max-heap for lower half, min-heap for upper half) to maintain the median in O(log N) per insertion, with a queue to track the sliding window of N comments. Discuss trade-offs between this approach and alternatives like balanced BSTs or order-statistic trees, and handle edge cases like even/odd counts and duplicate ratings.

Pro tip: Mention that you would use lazy deletion to handle expired comments from the sliding window, avoiding O(N) removal from heaps. Also, proactively discuss how to handle ties and the median definition for even counts (average of two middle values).

1. Clarify requirements and constraints

Ask about the expected volume of comments, whether N is fixed or can change, and the definition of median for even counts. Confirm if ratings are integers and if there are any memory constraints.

2. Propose a data structure for median maintenance

Describe using two heaps: a max-heap for the lower half and a min-heap for the upper half, keeping their sizes balanced. This allows O(log N) insertion and O(1) median retrieval.

3. Incorporate sliding window for most recent N comments

Use a queue to track the order of comments. When a new comment arrives, add it to the heaps and enqueue it; if the queue size exceeds N, remove the oldest comment from the heaps (using lazy deletion) and dequeue it.

4. Handle edge cases and lazy deletion

Explain how to handle expired comments that are still in the heaps: maintain a count of invalid entries and skip them when they reach the top during rebalancing or median retrieval.

5. Discuss trade-offs and alternatives

Compare with other approaches like balanced BSTs (e.g., order-statistic tree) or Fenwick trees over a rating range. Highlight time/space complexity and suitability for streaming data.

Key Points to Mention

  • Two-heap approach for median maintenance with O(log N) insertion and O(1) query.
  • Sliding window using a queue and lazy deletion to avoid O(N) removal.
  • Handling even number of comments: median as average of two middle values.
  • Time complexity: O(log N) per insertion, O(1) per median query, O(N) space.
  • Trade-offs: two heaps vs. balanced BST vs. Fenwick tree (if rating range is small).
  • Edge cases: empty window, duplicate ratings, and dynamic N.

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