← Practice · Heap

Find Median from Data Stream Hard

Design a class MedianFinder that receives numbers one at a time and can report the median of everything received so far. - addNum(num): add an integer to the stream. - findMedian(): return the median (the middle value, or the mean of the two middle values when the count is even).

Examples

  • addNum(1), addNum(2), findMedian() -> 1.5, addNum(3), findMedian() -> 2.

Define a class named MedianFinder in JavaScript.

00:00▶ Visualize

JavaScript · Tab indents · Ctrl/⌘+Enter runs the tests