Find Median from Data Stream
Updated
Problem statement
Process a finite sequence of operations while maintaining every integer added so far. Each row in operations has one of these forms:
["add", value]inserts the integer represented byvalue.["median"]queries the current median.
For an odd number of stored values, the median is the middle value after sorting. For an even number, it is the arithmetic mean of the two middle values. Return a double[] containing the median-query results in encounter order. Add operations produce no output.
Function
processMedianOperations(operations: String[][]) → double[]Examples
Example 1
operations = [["add","5"],["median"],["add","1"],["median"],["add","9"],["median"]]return = [5.0,3.0,5.0]The stored multisets at the three queries are [5], [1,5], and [1,5,9], whose medians are 5, 3, and 5.
Example 2
operations = [["add","-4"],["add","8"],["median"],["add","8"],["median"],["add","20"],["median"]]return = [2.0,8.0,8.0]The queries observe [-4,8], [-4,8,8], and [-4,8,8,20]. Their medians are 2, 8, and 8.
Example 3
operations = [["add","1000000000"],["add","999999999"],["median"]]return = [999999999.5]The two middle values are 999999999 and 1000000000, so their arithmetic mean is 999999999.5.
Constraints
1 <= operations.length <= 2000- Every row is either
["add", value]or["median"]. - Each added value is an integer from
-1000000000through1000000000. - Every median query occurs after at least one add operation.
- At least one median query appears.
More Bloomberg LP Onsite Interview questions
- N-ary Index-Path Encoder and DecoderPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Top K Frequent WordsONSITE INTERVIEW · Seen Nov 2026
- Trapping Rain WaterPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Word SearchOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Design Browser HistoryPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Mutable Stock Price Top KPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Add Two Numbers IIPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Decode StringPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Minimum Replacements to Make Two Strings AnagramsPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- Currency Conversion RatePHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
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
- 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
- Worst-Performing Ads by CTR in a Campaign TreeUber · PHONE SCREENUber interview questions