Hard · Streams & I/O

Find median in stream

Given a stream of integers that arrive one at a time, return after each arrival the median of all values seen so far: the middle value when the count is odd, the mean of the two middle values when it is even.

Examples

Example 1

{
  "nums": [1, 2, 3, 4, 5]
}

Output: [1, 1.5, 2, 2.5, 3]

Example 2

{
  "nums": [5, 15, 1, 3]
}

Output: [5, 10, 5, 4]

Rebuild it in the studio

Read every interview problem free. Ten rooms need no account. A token opens a problem in full — Pro never counts.

More Streams & I/O problems