#295

Find Median from Data Stream

HardTwo HeapsHas vizHeapDesign

Problem

Find Median from Data Stream

You must solve the interview problem titled "Find Median from Data Stream".

Difficulty: Hard. Topics: Heap, Design. Primary pattern: Two Heaps.

Read the title as the specification: implement the exact behavior it names, return the correct output type, and aim for an interview-optimal Two Heaps solution. Use the examples to lock input/output format, then confirm edge cases (empty, single element, duplicates, extremes).

Example 1

Input: sample input for Find Median from Data Stream

Output: sample output for Find Median from Data Stream

Apply a Two Heaps approach and match the required return type.

Example 2

Input: edge-case input for Find Median from Data Stream

Output: edge-case output for Find Median from Data Stream

Verify empty/single/boundary behavior.

Constraints

  • Interview-scale limits for a Hard Two Heaps problem.
  • Handle empty and single-element inputs when they are valid.
  • Prefer the optimal asymptotic complexity expected in interviews.
Loading visualization…

Explanation

Find Median from Data Stream is a Hard interview problem tagged [Heap, Design]. The winning approach is typically **Two Heaps**.

Intuition

Heaps maintain order statistics under streaming inserts.

Approach

Balance a max-heap (lower half) and min-heap (upper half) to read the median in O(1).

  1. Push to appropriate heap.
  2. Rebalance sizes.
  3. Median is root or average of roots.

Solution

Pattern-level skeleton only — not this problem’s exact answer. Adapt it to the statement.

Pattern templateTwo Heaps

Edge cases

  • •Empty or single-element input
  • •Duplicates and ties
  • •Maximum constraints / overflow
  • •Disconnected or unreachable states (for graphs)

Complexity

Time: O(n log n)

Space: O(n)

Before coding, restate the problem, name the pattern (Two Heaps), and write the invariant or DP state in one sentence.

Learn the topic

Got it?

Mark #295 Find Median from Data Stream as understood when the pattern clicks.