KatInterview

Running Median with Power-of-Two Bucket Tracking

Heaps, Sorting & SearchAuto-graded · 24 test cases
MediumAuto-gradedHeaps, Sorting & Searchheapsmedianbit-manipulation

Last updated

Tests two-heap streaming median maintenance combined with exact integer floor-log₂ bucketing to place each median into its enclosing power-of-two interval.

Running Median with Power-of-Two Bucket Tracking

You are given a sequence of positive integers arriving one at a time. After each new value is consumed, compute two things:

  1. Running median — the median of all values seen so far. When the count of values is odd, the median is the single middle value. When the count is even, the median is the arithmetic mean of the two middle values (which may be a half-integer such as 3.5).

  2. Power-of-two interval — the unique pair [L, R] where L = 2^k and R = 2^(k+1) for the integer k satisfying 2^k ≤ median < 2^(k+1). In other words, L is the largest power of two that does not exceed the median, and R is the next power of two above it.

Return a list of results, one per value in the input. Each result is a two-element list [median, [L, R]].

Details

  • The input list contains only positive integers, so the median is always at least 1 and the interval always exists.
  • For a median that is itself an exact power of two (e.g., 4), the interval is [4, 8] — the half-open convention [2^k, 2^(k+1)) applies, placing the left endpoint in the bucket.
  • For a half-integer median such as 1.5, floor it to find k: floor(1.5) = 1, so the interval is [1, 2].
  • Return median as a Python int when the count is odd, and as a Python float (e.g., 1.5) when the count is even and the two middle values have an odd sum; return an int when the count is even and their sum is even.

Constraints

  • 1 ≤ len(values) ≤ 10^5
  • 1 ≤ values[i] ≤ 10^9
  • All elements are positive integers.
  • The returned list has exactly len(values) entries.
  • Each entry is [median, [L, R]] where L and R are positive integers and R = 2 * L.

Examples

Example 1

Input: running_median_with_power_interval([5, 15, 1, 3])

Output: [[5, [4, 8]], [10, [8, 16]], [5, [4, 8]], [4, [4, 8]]]

Example 2

Input: running_median_with_power_interval([1, 2])

Output: [[1, [1, 2]], [1.5, [1, 2]]]

Example 3

Input: running_median_with_power_interval([1, 3])

Output: [[1, [1, 2]], [2, [2, 4]]]

Example 4

Input: running_median_with_power_interval([2, 4, 8, 16])

Output: [[2, [2, 4]], [3, [2, 4]], [4, [4, 8]], [6, [4, 8]]]

Hints

Hint 1

Keep two heaps: a max-heap holding the smaller half of values seen so far, and a min-heap holding the larger half. After each insertion, rebalance so the max-heap is either the same size as the min-heap or exactly one element larger — that way the median is always at or near the top of the max-heap.

Hint 2

To avoid floating-point imprecision when the count is even, work with twice the median: add the two heap tops as integers. You only produce a float in the final step if that sum is odd.

Hint 3

To find k such that 2^k ≤ median < 2^(k+1), compute floor(median) and use its bit length. For a fraction p/q, find k by checking (1 << k) * q ≤ p and (1 << (k+1)) * q > p using only integer arithmetic — no math.log needed.