Reported September 2026
Amazonheap priority queue

Find Median from Data Stream

Reported by candidates from Amazon's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Amazon OA. Under 2s to a working solution.
Founder's read

The edge case that sinks most first attempts at this Amazon problem is the even-count median, where two huge integers get averaged and a careless int sum overflows. It was reported in September 2026, and it's the classic Find Median from Data Stream wrapped in a string-array operations format. Each row is either ["add", value] or ["median"], and you return a double[] of the query results only. The pattern is two heaps. If you've seen it, it's ten minutes. If you blank on the heap balancing, StealthCoder is the invisible safety net running during the live OA.

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 two heaps. Keep a max-heap for the lower half and a min-heap for the upper half. On each add, push into the max-heap, move its top to the min-heap, then rebalance so the max-heap is equal in size or has one extra element. Odd count: the median is the max-heap top. Even count: average the two tops. The pitfall is overflow. Values reach 1000000000, so adding two of them in a 32-bit int breaks. Cast to long or double before summing, and divide by 2.0, not 2. Also remember the input is strings, so parse each value, and only append output on median rows. With at most 2000 operations, re-sorting a list each query would pass, but heaps are the answer they want. If the rebalancing logic slips mid-assessment, StealthCoder can supply a working version.

The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.

If this hits your live OA

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. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.

Get StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as find median from data stream. If you have time before the OA, drill that.

⏵ The honest play

You've seen the question. Make sure you actually pass Amazon's OA.

Amazon reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Find Median from Data Stream FAQ

What's the trick to Find Median from Data Stream?+

Use two heaps. A max-heap holds the smaller half and a min-heap holds the larger half. Keep their sizes within one of each other. The median is either the top of the max-heap or the average of both tops. Each add costs O(log n) and each median query is O(1).

How hard is this one really?+

It's a known medium-hard problem, but the pattern is famous. The twist here is the string-array input and the double[] output. Once you parse the operations and handle the even-count average, the logic is standard. Most of the risk is in rebalancing and overflow.

Can I just sort the list on every median query?+

With at most 2000 operations it would likely run fast enough, since 2000 sorts of up to 2000 items is manageable. But it's the naive route, and it may not read well to reviewers. Heaps show you know the intended pattern. Use sorting only as a fallback if you're stuck.

What overflow or precision edge cases should I watch?+

Values go up to 1000000000 in magnitude, so summing two of them can overflow a 32-bit int. Convert to long or double before adding, then divide by 2.0. Example 3 checks this: 999999999 and 1000000000 should give 999999999.5.

How do I prepare for this in 48 hours?+

Write the two-heap solution from scratch twice. Practice the add, balance, and median steps until they're automatic. Then test with the examples, including negative numbers and duplicates like [-4,8,8]. Also practice parsing string values into integers and collecting only median results in order.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Amazon.

OA at Amazon?
Invisible during screen share
Get it