Maintain the Median of an Integer Stream with Two Heaps

Quick Overview

Maintain streaming medians with balanced lower and upper heaps. Prove the ordering and size invariants, explain heap costs, and distinguish fast queries from bounded-memory streaming.

Maintain the Median of an Integer Stream with Two Heaps

Company: SIG (Susquehanna)

Role: Data Scientist

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

Support addNum(value), which adds an integer to a stream, and findMedian(), which returns the median of all values received so far. For an even count, return the arithmetic mean of the two middle values. ### Constraints and Clarifications The source names the standard data-stream median problem and asks heap follow-ups. Use the [official reference](https://leetcode.com/problems/find-median-from-data-stream/description/) for the practice interface: values between-100000 and100000, at most50000 operations, and at least one value before every median query. These bounds are reference context, not separately recalled interview constraints. Serialized add operations emit null; queries emit numeric medians. The reference accepts absolute error within1e-5, though integer and half-integer medians in this domain are exactly representable in ordinary binary floating point. ### What a Strong Answer Covers Maintain a max-heap for the lower half and a min-heap for the upper half, prove ordering and balance invariants, and explain insertion and query complexity. ### Follow-up Questions - What heap operations are required, and why are they logarithmic? - Does the two-heap method use bounded memory for an arbitrarily long stream?

Overview: Maintain streaming medians with balanced lower and upper heaps. Prove the ordering and size invariants, explain heap costs, and distinguish fast queries from bounded-memory streaming.

SIG (Susquehanna) logo
SIG (Susquehanna)
Aug 28, 2026
mediumData ScientistTechnical ScreenCoding & Algorithms
0
0

Support addNum(value), which adds an integer to a stream, and findMedian(), which returns the median of all values received so far. For an even count, return the arithmetic mean of the two middle values.

Constraints and Clarifications

The source names the standard data-stream median problem and asks heap follow-ups. Use the official reference for the practice interface: values between-100000 and100000, at most50000 operations, and at least one value before every median query. These bounds are reference context, not separately recalled interview constraints. Serialized add operations emit null; queries emit numeric medians. The reference accepts absolute error within1e-5, though integer and half-integer medians in this domain are exactly representable in ordinary binary floating point.

What a Strong Answer Covers Guidance

Maintain a max-heap for the lower half and a min-heap for the upper half, prove ordering and balance invariants, and explain insertion and query complexity.

Follow-up Questions Guidance

  • What heap operations are required, and why are they logarithmic?
  • Does the two-heap method use bounded memory for an arbitrarily long stream?