Find Median from Data Stream
Updated
Problem statement
Numbers arrive one at a time. Build a structure that stores them and can report their median at any moment. The structure starts empty.
The median of a group of numbers is found after sorting them: it is the middle number when the count is odd, and the average of the two middle numbers when the count is even, so it can end in .5.
You get two arrays of the same length: operations and values. Run the operations from left to right. At index i, operations[i] is one of these:
"addNum": addvalues[i]to the structure."findMedian": add the median of all numbers added so far to the result. The stored numbers do not change.
Return the results of all "findMedian" operations, in the order they ran.
Each "addNum" should take O(log n) time and each "findMedian" should take O(1) time, where n is the number of values added so far.
Function
runMedianFinder(operations: String[], values: int[]) → double[]Examples
Example 1
operations = ["addNum","findMedian","addNum","findMedian","addNum","addNum","findMedian","addNum","findMedian"]values = [6,0,10,0,2,7,0,3,0]return = [6.0,8.0,6.5,6.0]After 6 the only number is 6. After 10 the two middle numbers are 6 and 10, so the median is 8.0. After 2 and 7 the sorted numbers are [2,6,7,10] and the median is (6 + 7) / 2 = 6.5. After 3 the sorted numbers are [2,3,6,7,10] and the middle one is 6.
Example 2
operations = ["addNum","addNum","findMedian","addNum","findMedian","addNum","findMedian"]values = [-4,-4,0,5,0,-1,0]return = [-4.0,-4.0,-2.5]The first two numbers are both -4, so the median is -4.0. After 5 the sorted numbers are [-4,-4,5] and the middle one is still -4. After -1 they are [-4,-4,-1,5] and the median is (-4 + -1) / 2 = -2.5.
Example 3
operations = ["addNum","addNum","findMedian","findMedian"]values = [3,4,0,0]return = [3.5,3.5]The median of 3 and 4 is 3.5. Asking for the median again without adding a number gives the same answer.
Constraints
1 <= operations.length <= 10000values.length == operations.length- Each
operations[i]is"addNum"or"findMedian". -105 <= values[i] <= 105- When
operations[i]is"findMedian",values[i]is0and is not used. - Every
"findMedian"runs after at least one"addNum".
More Zillow Onsite Interview questions
- Valid ParenthesesPHONE SCREEN · ONSITE INTERVIEW · Seen Jul 2025
- Build a Hash TablePHONE SCREEN · ONSITE INTERVIEW · Seen Feb 2023
- Spiral MatrixPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2022
- First Subarray with the Largest SumOA · ONSITE INTERVIEW · Seen Aug 2021
- Count Duplicate CharactersONSITE INTERVIEW · Seen Oct 2020
- Decision Tree Classifier from ScratchONSITE INTERVIEW · Seen Nov 2019
- LRU CachePHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2016
- Tic-Tac-Toe Winner on a Huge BoardPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2016
- Most Frequent Three-Page SequencesPHONE SCREEN · ONSITE INTERVIEW · Seen Aug 2016
- Maximum Path Sum in a GridONSITE INTERVIEW · Seen Apr 2016
Similar questions at other companies
- Find Median from Data StreamSusquehanna International Group (SIG) · PHONE SCREENSusquehanna International Group (SIG) interview questions
- Top K With a Deterministically Noisy Comparator (MLE)Mercor · ONSITE INTERVIEWMercor interview questions
- Find Median from Data StreamAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Find Median from Data StreamXPeng Motors · PHONE SCREEN
- Find Median from Data StreamBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- Median of a Number StreamAdobe · PHONE SCREENAdobe interview questions
- Running Stream MediansCitadel · ONSITE INTERVIEWCitadel interview questions
- Streaming Median of Trade PricesXantium · PHONE SCREEN
- Top K Pins with Insert and QueryPinterest · PHONE SCREENPinterest interview questions
- Alert Engine with a Repeat IntervalChronosphere · PHONE SCREEN