FreeCareerPath
Build the PrimitiveHard

Find Median from Data Stream

Implement `MedianFinder` with `add_num(num)` and `find_median()` returning the median of everything added so far, as a float. Keep a max-heap of the lower half and a min-heap of the upper half. Fix an invariant — for example the lower half never differs in size from the upper by more than one — and restore it after every insert. The median is then either one root or the average of both. Push into one heap and move its root across; pushing directly into the "right" heap without the transfer is what breaks the ordering.

What to expect: A timer starts when you begin. Edit the starter code, run it against the test suite as many times as you like, then finish when you're done. The reference solution and interviewer follow-up questions unlock only after you finish.

Log in to start this session

Timed sessions and your results for "Find Median from Data Stream" are saved to your account — logging in takes a few seconds.