Dsa
Heap
Easy
Median of a Stream
You are tasked with calculating the median of a stream of integers. As each integer is read from input, it should be processed to update the median. You will utilize two heaps: a max-heap for the lower half of numbers and a min-heap for the upper half.
Input format:
- The first line contains an integer N, the total number of integers in the stream.
- The next N lines each contain one integer.
Output format:
- After each integer input, print the current median.
Example:
Input:
5
1
5
2
4
3
Output:
1
3.0
2
3.0
3
Key concepts
heapmediandata-stream
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now