Find Median from Data Stream
Reported by candidates from Bloomberg's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Bloomberg reported this one in November 2025, and it looks friendly until you think about the input. Up to 2000 operations, each one an add or a median query. Re-sorting the whole list on every query is the first idea that comes to mind, and it's the one the problem is built to punish as the list grows. The pattern is two heaps, a max-heap for the lower half and a min-heap for the upper half. If the OA is tomorrow, you need that split memorized cold. StealthCoder runs invisibly as a safety net on the live OA if your mind goes blank on the rebalancing step.
The problem
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 by value. ["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 -1000000000 through 1000000000. Every median query occurs after at least one add operation. At least one median query appears.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is keeping two halves of the data. A max-heap holds the smaller half, a min-heap holds the larger half, and the sizes never differ by more than one. On add, push into the max-heap, move its top to the min-heap, then if the min-heap is larger, move its top back. Median is the max-heap top when the sizes are odd, or the mean of both tops when even. Each add is O(log n) and each query is O(1). The common pitfall is integer overflow and integer division. Values reach 1000000000, so add two of them as long or double before halving, or you'll print 999999999.0 instead of 999999999.5. Also parse the string values into integers and ignore the add rows in the output. If you freeze on the rebalance logic mid-assessment, StealthCoder can hand you the working version.
Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.
You can drill Find Median from Data Stream cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as find median from data stream. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Bloomberg's OA.
Bloomberg reuses patterns across OAs. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Find Median from Data Stream FAQ
What's the trick for Find Median from Data Stream?+
Use two heaps. A max-heap stores the lower half and a min-heap stores the upper half. Keep their sizes within one of each other. The median is either the top of the larger heap or the average of both tops. That gives O(log n) adds and O(1) medians.
Can I just sort the list on every median query?+
With at most 2000 operations, it would probably pass here. But it's O(n log n) per query, and an interviewer or hidden test may expect the heap solution. Insert with binary search is a middle ground. Write the two-heap version if you can, since it's the answer they're looking for.
What edge cases break this problem?+
Overflow and rounding. Values go up to 1000000000 in magnitude, so summing two in a 32-bit int can overflow. Cast to double or long before dividing by 2. Duplicates are fine because it's a multiset. Negative numbers work with no special handling in the heaps.
How do I handle the String[][] input format?+
Check the first element of each row. If it's add, parse the second element with Integer.parseInt and insert it. If it's median, compute the result and append it to your output list. Convert that list to a double[] at the end. Add rows produce nothing.
How do I prepare for this in 48 hours?+
Write the two-heap median solution from scratch twice, without looking. Focus on the add-then-rebalance order and the odd/even median logic. Then test it against the three examples, especially the 999999999.5 case. That covers the whole problem and its variants.