2Brute2Best
πŸ”₯0

Find Median from Data Stream

HardΒ·LeetCode #295 Pro OnlyBasic Reinforcement
LeetCode
01

Problem Statement

Design a class MedianFinder that holds a stream of integers and can report their median at any time.

  • MedianFinder() starts with no numbers.
  • addNum(num) adds num to the stream.
  • findMedian() returns the median of every number added so far. With an odd count it is the middle number. With an even count it is the average of the two middle numbers, which can be a fraction such as 1.5.

The two methods can be called in any order, so findMedian must give the right answer right after every addNum.

Input

A sequence of addNum(num) and findMedian() calls. Every findMedian() comes after at least one addNum.

Output

For each findMedian(), the median as a double. addNum returns nothing.

Example 1
Input: addNum(1), addNum(2), findMedian(), addNum(3), findMedian()
Output: 1.5, 2.0

The numbers 1 and 2 have median (1 + 2) / 2 = 1.5. After adding 3, the numbers 1, 2, 3 have median 2.

Example 2
Input: addNum(-5), findMedian(), addNum(10), findMedian()
Output: -5.0, 2.5

One number: its own value, βˆ’5. Then βˆ’5 and 10 have median (βˆ’5 + 10) / 2 = 2.5.

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
  • β€’ Follow-up: If all integer numbers from the stream are in the range [0, 100], how would you optimize your solution?
  • β€’ Follow-up: If 99% of all integer numbers from the stream are in the range [0, 100], how would you optimize your solution?
02

Pattern Recognition Clues

Recognising these key signals in the problem description immediately identifies the pattern:

β€œaddNum and findMedian as separate calls”
β†’ The data arrives one call at a time, so the structure must stay ready for any call order.
β€œmedian”
β†’ The middle of the sorted numbers. Only the boundary between the two halves matters.
β€œfindMedian must be fast”
β†’ Keep the two tops ready, so reading the median is O(1).
03

Approach Complexity Ladder

Brute Force β€” store everything, sort inside findMedianO(N log N)O(N)

addNum appends the number; findMedian copies, sorts and reads the middle

Better β€” keep a sorted vector, insert each number in placeO(N)O(N)

addNum finds the slot with binary search and inserts; findMedian reads the middle in O(1)

Optimal β€” two heaps: addNum O(log n), findMedian O(1)O(log N)O(N)

A max-heap for the lower half and a min-heap for the upper half; the two tops are the middle

Brute Force Intuition

addNum just appends the number to a vector. findMedian sorts that vector and reads the middle, exactly as in the first question. Adding is O(1), but every findMedian pays for a full sort.

Recognition rule for this sub-pattern:

Push into one heap, move its top to the other, rebalance sizes.
Remember this Β· Two Heaps
Trigger
Running median, sliding median, choosing from two pools (available vs locked).
Think
Can I split the data into two halves and only care about the boundary between them?
Key question
How do I rebalance so the sizes differ by at most one?
Brute2Best Pro Content

Unlock Complete Visual Walkthrough & Optimal C++

The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like Two Heaps translate into production architectures and low-level memory mechanics.