KatInterview

Running Median via Dual-Heap Simulation

Data Structure DesignAuto-graded · 18 test cases
MediumAuto-gradedData Structure Designheapsdata-structure-designonline-algorithms

Last updated

Tests whether a candidate can maintain a live median over an integer stream in O(log n) per insert using two heaps balanced around the midpoint.

Running Median via Dual-Heap Simulation

You are given a sequence of operations that build up a collection of integers one value at a time and periodically ask for the current median. Simulate those operations and collect every median result.

Definitions

Given a collection of numbers sorted in non-decreasing order:

  • Odd size: the median is the single element at the center position.
  • Even size: the median is the arithmetic mean of the two elements nearest the center.

Input Format

You receive two lists of equal length:

  • operations — a list of strings, each either "add" or "median".
  • arguments — a list of lists. For an "add" operation, arguments[i] contains exactly one integer, the value to insert. For a "median" operation, arguments[i] is an empty list.

Process the operations in order, starting from an empty collection.

Output Format

Return a list with one entry per operation:

  • For every "add" operation, the corresponding entry is None.
  • For every "median" operation, the corresponding entry is the median of all values inserted so far, as a float.

If an operation string is neither "add" nor "median", raise a ValueError.

Constraints

  • Integer values supplied to "add" are within the range of 32-bit signed integers and may be negative or repeated.
  • The total number of operations is at most 10^5.
  • "median" is never the first operation; at least one "add" always precedes it.
  • The returned median must be arithmetically exact. When the collection size is even, return the true average of the two central values (e.g., 2.5, not 2).
  • "add" and "median" calls may be freely interleaved in any order.
  • Your solution must handle each "add" in roughly O(log n) time and each "median" in O(1) time. A full re-sort on every query is too slow for the given input size.

Examples

Example 1

Input: median_tracker(['add', 'median', 'add', 'median', 'add', 'median', 'add', 'median'], [[1], [], [3], [], [2], [], [10], []])

Output: [None, 1, None, 2, None, 2, None, 2.5]

Example 2

Input: median_tracker(['add', 'add', 'add', 'add', 'median', 'median'], [[-5], [-5], [0], [10], [], [], []])

Output: [None, None, None, None, -2.5, -2.5]

Hints

Hint 1

Think about what information you actually need to compute the median — you only care about the one or two values at the center of the sorted order, not the entire sorted list. What data structures give you instant access to a maximum or a minimum?

Hint 2

Two heaps work well here: one holding the smaller half of the values, one holding the larger half. If you keep their sizes within one of each other at all times, the tops of the two heaps are exactly the values you need for the median.

Hint 3

Python's heapq is a min-heap only. To simulate a max-heap for the lower half, store each value negated — push -x instead of x, and negate again when you peek or pop.