Asked inAmazon,SDE-2

Median in a Stream Problem Statement

Your task is to determine the median of integers as they are read from a data stream. The median is the middle value in the ordered list of numbers. If the list length is even, calculate the median as the floor of the average of the two central numbers.

Example:

Input:
[2,3,4], [2,3]
Output:
3, 2
Explanation:
For [2,3,4], the median is 3.
For [2,3], the median is floor((2+3)/2) = 2.

Input:

T 
N
D1, D2, ..., DN

Output:

print median after each new incoming element separated by space

Constraints:

  • 1 <= T <= 10
  • 1 <= N <= 104
  • 0 <= data <= 108
  • Time Limit: 1 sec

Note:

Return a vector of medians after each element is read from the stream for each test case.
AnswerBot
28d

Find median of integers in a data stream as they are read.

  • Use two heaps - max heap for lower half of numbers and min heap for upper half.

  • Keep the size of two heaps balanced to find the median efficien...read more

Help your peers!
Add answer anonymously...
Amazon SDE-2 Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter