KatInterview

Running Median Tracker Over an Operation Log

Data Structure DesignAuto-graded · 14 test cases
MediumAuto-gradedData Structure Designheaptwo-heapsstreaming

Last updated

Process a log of insert and query operations on an integer stream, returning the current median for each query, using two heaps for efficient updates.

You are building a tracker that receives integers one at a time and can report the median of everything received so far. Instead of working with a class directly, your function replays a recorded log of operations and returns what each operation produced.

Median definition

Take all integers received so far and sort them.

  • If the count is odd, the median is the middle value.
  • If the count is even, the median is the average of the two middle values.

Operations

The log is given as two parallel lists, operations and arguments. The element at position i of arguments is the argument list for the operation at position i of operations.

OperationArgument listEffectOutput entry
"MedianFinder"[]Creates the empty tracker. It always appears first.None
"add_num"[x]Adds the integer x to the stream.None
"find_median"[]Computes the median of all integers added so far.the median as a float

Task

Implement median_tracker_log(operations, arguments). It should return a list with the same length as operations, where entry i is the output of operation i according to the table above.

Medians must be returned as floats (for example 2.0, not 2). When the count is even, compute the average without losing precision for very large or very small values; in Python this is naturally safe, but your logic should not rely on any fixed-width integer wraparound.

The structure should be efficient: inserting a number should take about O(log n) time and answering a median query about O(1) time.

Constraints

  • Each added value x satisfies -2^31 <= x <= 2^31 - 1.
  • The log contains at most 100,000 operations in total.
  • find_median is only issued after at least one number has been added.
  • "MedianFinder" appears once, as the first operation.
  • len(operations) == len(arguments).
  • Duplicates and negative values may appear.

Examples

Example 1

Input: median_tracker_log(['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: median_tracker_log(['MedianFinder', 'add_num', 'add_num', 'find_median'], [[], [2147483647], [2147483647], []])

Output: [None, None, None, 2147483647]

Hints

Hint 1

Sorting everything on each query is too slow. Only the values around the middle of the sorted order matter for the median.

Hint 2

Keep the smaller half of the numbers in a max-heap and the larger half in a min-heap, with every value in the first half no larger than every value in the second.

Hint 3

Keep the sizes equal, or let the lower half hold exactly one extra element. After each insertion, move at most one element between heaps to restore this. In Python, simulate a max-heap by storing negated values.