← Amazon Interview Insights

Amazon·Data Scientist·Onsite - Coding / Algorithms·Senior

SeniorPrefer not to say
Apr 2026

Summary

Senior data scientist round at Amazon, algorithmic focus, they put you on the clock and expect clean code plus complexity analysis. Not a vibe check, they want you to actually know your stuff.

Questions Asked (1)

Q1

Design a data structure that supports inserting integers from an endless stream and can return the median at any point, with O(log n) insertion time and O(1) median query time. Walk through your implementation and complexity.

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

I knew the two-heap trick going in but still fumbled the balancing logic under pressure.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use two heaps (a max-heap for the lower half and a min-heap for the upper half) to maintain the median in O(1) query time. Insert each new element into the appropriate heap and rebalance so their sizes differ by at most one. Explain how this achieves O(log n) insertion and O(1) median retrieval.

Pro tip: Mention that this two-heap approach is the standard solution and is used in real-time analytics at Amazon; also discuss how it handles endless streams without storing all data, which is crucial for scalability.

1. Clarify requirements and constraints

Confirm that insertion must be O(log n) and median query O(1), and that the stream is endless so memory should be bounded. Ask if duplicates are allowed and if the median definition for even count is the average of two middle values.

2. Propose the two-heap data structure

Describe using a max-heap to store the smaller half of numbers and a min-heap for the larger half. The median is either the top of the max-heap (odd total) or the average of the tops of both heaps (even total).

3. Detail insertion and rebalancing

Explain the insertion algorithm: add to max-heap if the number is less than or equal to its top, else to min-heap; then rebalance by moving the top element from the larger heap to the smaller if size difference exceeds 1. This ensures O(log n) time due to heap operations.

4. Analyze complexity and trade-offs

State that insertion is O(log n) because each heap operation is O(log n) and rebalancing is O(log n). Median query is O(1) since it only involves peeking at heap tops. Discuss space complexity O(n) and compare with alternatives like sorted arrays (O(n) insertion) or balanced BSTs (O(log n) insertion but O(log n) median).

5. Discuss extensions and edge cases

Mention handling of empty stream, single element, and even/odd counts. Optionally, discuss how to adapt for sliding window medians or approximate medians for massive streams using sketches.

Key Points to Mention

  • Two heaps: max-heap for lower half, min-heap for upper half
  • Rebalancing condition: size difference at most 1
  • Time complexity: O(log n) insertion, O(1) median query
  • Space complexity: O(n) for storing all elements
  • Handling even count: median is average of max-heap top and min-heap top
  • Comparison with other data structures (e.g., sorted list, balanced BST)

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