PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates understanding of streaming data structures, median maintenance, and algorithmic complexity in the Coding & Algorithms domain by requiring efficient insertions (O(log n)) and constant-time median queries (O(1)).

  • Medium
  • Amazon
  • Coding & Algorithms
  • Data Scientist

Design Efficient Data Structure for Median Retrieval

Company: Amazon

Role: Data Scientist

Category: Coding & Algorithms

Difficulty: Medium

Interview Round: Technical Screen

##### Scenario Senior problem-solving round assessing algorithmic thinking under time pressure. ##### Question Design a data structure that supports inserting integers from an endless stream and returning the median in O(log n) per insertion and O( 1) per query. Provide code and complexity analysis. ##### Hints Two heaps approach; keep sizes balanced; median at roots.

Quick Answer: This question evaluates understanding of streaming data structures, median maintenance, and algorithmic complexity in the Coding & Algorithms domain by requiring efficient insertions (O(log n)) and constant-time median queries (O(1)).

Design a data structure that ingests integers from an endless stream and can return the running median efficiently. It must support two operations: - addNum(num): add the integer num to the structure. - findMedian(): return the median of all integers added so far. With an even count the median is the average of the two middle values; with an odd count it is the single middle value. If no numbers have been added, return null. Your insert must run in O(log n) and each median query in O(1). To make the design executable here, implement a single function/method that replays a list of operations and returns one entry per findMedian call. Each operation is a list: ["addNum", num] inserts num, and ["findMedian"] queries the current median. Return a list whose i-th element is the median produced by the i-th findMedian operation (use null/None when the structure is empty). Medians are returned as floating-point values (e.g. an integer median of 4 is 4.0). Hint: keep two heaps — a max-heap holding the smaller half and a min-heap holding the larger half — and rebalance after each insert so their sizes differ by at most one. The median is then read off the heap roots in O(1).

Constraints

  • 1 <= number of operations <= 5 * 10^4
  • -10^5 <= num <= 10^5 for each addNum
  • findMedian on an empty structure returns null
  • addNum must be O(log n); findMedian must be O(1)

Examples

Input: ([["addNum", 1], ["addNum", 2], ["findMedian"], ["addNum", 3], ["findMedian"]],)

Expected Output: [1.5, 2.0]

Explanation: After adding 1 and 2 the two middle values are 1 and 2, so the median is (1+2)/2 = 1.5. After adding 3 the values are [1,2,3] and the single middle value is 2.0.

Input: ([["addNum", 5], ["findMedian"], ["addNum", 5], ["findMedian"], ["addNum", 5], ["findMedian"]],)

Expected Output: [5.0, 5.0, 5.0]

Explanation: All inserted values are equal, so the median is 5.0 regardless of count, covering the duplicate-values case.

Input: ([["findMedian"]],)

Expected Output: [null]

Explanation: Edge case: querying before any number is added returns null because the structure is empty.

Input: ([["addNum", -10], ["addNum", -20], ["addNum", -30], ["findMedian"]],)

Expected Output: [-20.0]

Explanation: Negative values [-10,-20,-30] sorted are [-30,-20,-10]; the single middle value is -20.0, verifying correct handling of negatives.

Input: ([["addNum", 4], ["findMedian"], ["addNum", 1], ["addNum", 7], ["addNum", 2], ["findMedian"], ["addNum", 9], ["findMedian"]],)

Expected Output: [4.0, 3.0, 4.0]

Explanation: Median of [4] is 4.0; of [1,2,4,7] (sorted) is (2+4)/2 = 3.0; of [1,2,4,7,9] is the middle value 4.0 — an interleaved insert/query sequence.

Hints

  1. Maintain two heaps: a max-heap for the smaller half of the numbers and a min-heap for the larger half.
  2. After each insert, rebalance so the heaps' sizes differ by at most one — push the offending root across when one heap grows too large.
  3. If the heaps are equal in size, the median is the average of the two roots; otherwise it is the root of the larger heap. Both are O(1) reads.
Last updated: Jun 25, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Implement Top-p (Nucleus) Sampling in NumPy - Amazon (medium)
  • Implement Multi-Head Attention from Scratch in NumPy - Amazon (medium)
  • Detect and Break a Cycle in a Singly Linked List - Amazon (medium)
  • Caesar Cipher with Translation-Table Optimization - Amazon (medium)
  • Minimum Drone Delivery Time on a Ring of Hubs - Amazon (medium)