Applied AI Engineer (LLM) coding learning path Level 3 This level · 8 of 27

Find Median from Data Stream

Hard Heap Sorting

Problem

Design a structure that accepts integers one at a time and can return the median of all values seen so far. If the count is even, return the average of the two central values as a double.

Original: LeetCode 295: Find Median from Data Stream.

Examples

Example 1

Input`MedianFinder: addNum(1), addNum(2), findMedian(), addNum(3), findMedian()`
Output`[null, null, 1.5, null, 2.0]`

Constraints

  • -10^5 <= num <= 10^5
  • There will be at least one element in the data structure before calling findMedian.
  • At most 5 * 10^4 calls will be made to addNum and findMedian.

Hints

Solution

Tried it yourself? The approach, complexity, and code stay hidden until you choose.

Sources (2)
  1. LeetCode 295: Find Median from Data Streamleetcode.com
  2. Java SE 17 API: PriorityQueuedocs.oracle.com
Previous
Next