Notice Period

Running Median Monitor

hard

A factory line streams sensor readings all shift, and quality control wants the median reading on demand — averages hide outliers; the median doesn't flinch.

Operations format. Your function receives one argument ops: a list of operations, each itself a list — ["add", x] records a reading, ["median"] asks for the current median. Return a list with one entry per operation: None for each add, and the median for each median query. With an odd count of readings the median is the middle value; with an even count it is the average of the two middle values (may be fractional). Every median call has at least one reading recorded.

Example 1
in ops = [["add", 1], ["add", 2], ["median"], ["add", 3], ["median"]]
out [None, None, 1.5, None, 2]
After {1, 2} the median is (1 + 2) / 2 = 1.5; after {1, 2, 3} it is 2.
Example 2
in ops = [["add", 5], ["median"]]
out [None, 5]
Constraints
  • · 1 <= len(ops) <= 2000
  • · -10^5 <= x <= 10^5
  • · median is never queried on an empty stream
similar problem on LeetCode ↗
solution.pyloading python…
test results
Hit ▶ Run to test your code against the visible cases.