KatInterview

Running Median Tracker with Dual-Heap Design

Data Structure DesignAuto-graded · 17 test cases
MediumAuto-gradedData Structure Designheaptwo-heapsdata-stream

Last updated

Tests the ability to maintain an efficient two-heap structure that yields the current median of a growing integer stream in O(log n) per insertion and O(1) per query.

Running Median Tracker

You are building a statistics module for a real-time data pipeline. Integers arrive one at a time, and after each batch of inserts, the pipeline queries the current median of every integer received so far.

Design a class RunningMedian with the following interface:

  • RunningMedian() — sets up the internal state (no return value).
  • add_num(val) — records one new integer from the stream (no return value).
  • find_median() — returns the median of all integers recorded so far, as a float.

Median definition

  • If the total count of recorded integers is odd, the median is the single middle value when all values are placed in sorted order.
  • If the total count is even, the median is the arithmetic mean of the two values closest to the middle.

Judge interface

The judge drives the class through two parallel lists:

  • operations — a list of operation name strings: "MedianFinder", "add_num", or "find_median".
  • arguments — a list of argument lists, one per operation. "MedianFinder" and "find_median" receive []; "add_num" receives [val].

Implement the function:

def running_median_tracker(operations: List[str], arguments: List[List[int]]) -> List[Optional[float]]:

It must return one output per operation:

  • None for "MedianFinder" and "add_num".
  • A float for "find_median".

Constraints

  • The total number of operations is between 1 and 100,000.
  • add_num is called at most 50,000 times.
  • find_median is called at most 50,000 times.
  • Every integer value passed to add_num is in the range [-10^5, 10^5].
  • find_median is never called before at least one add_num has been made.
  • An unknown operation name should raise ValueError.

Examples

Example 1

Input: running_median_tracker(['MedianFinder', 'add_num', 'add_num', 'find_median', 'add_num', 'find_median'], [[], [1], [2], [], [3], []])

Output: [None, None, None, 1.5, None, 2]

Example 2

Input: running_median_tracker(['MedianFinder', 'add_num', 'add_num', 'add_num', 'find_median', 'add_num', 'find_median'], [[], [5], [5], [5], [], [5], []])

Output: [None, None, None, None, 5, None, 5]

Hints

Hint 1

Think about what you actually need to know to answer a median query instantly — you only care about the one or two values nearest the centre of the sorted sequence, not the full order.

Hint 2

Try splitting the recorded values into two groups: a lower half and an upper half. If you can read the maximum of the lower half and the minimum of the upper half in O(1), you have everything you need for the median.

Hint 3

Python's heapq only gives a min-heap. You can simulate a max-heap on the lower half by storing each value negated, so the smallest stored number corresponds to the largest real value.