Find Median from Data Stream Problem
Find Median from Data Stream Problem — ExecCode Medium DSA Practice
Solve the Find Median from Data Stream problem on ExecCode. Free online medium DSA practice in Heap. Write and run code in Java, C++, Python — no signup required to run.
Problem description
Design a MedianFinder class that supports adding integers from a data stream one at a time and finding the median of all elements seen so far. The trace uses ops/vals arrays: ops names the call to make (MedianFinder to construct, addNum, findMedian) and vals holds its arguments; the result array records each call's return value (null for the constructor and addNum).
Examples
Input ops = ["MedianFinder", "addNum", "addNum", "findMedian", "addNum", "findMedian"], vals = [[], [1], [2], [], [3], []]; Output [null, null, null, 1.5, null, 2]. Input ops = ["MedianFinder", "addNum", "findMedian", "addNum", "findMedian"], vals = [[], [5], [], [10], []]; Output [null, null, 5, null, 7.5]. Input ops = ["MedianFinder", "addNum", "findMedian"], vals = [[], [42], []]; Output [null, null, 42]
Constraints
-10⁵ ≤ num ≤ 10⁵ At most 5 × 10⁴ calls will be made to addNum and findMedian findMedian is called only after at least one call to addNum
Practice Find Median from Data Stream free on ExecCode. Browse DSA problems, topic map, and placement guides.