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

Open every published interview problem, solution, scene, and memory room free, with no account.

More Streams & I/O problems