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
4mo

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!
Select
Add answer anonymously...

Amazon SDE-2 interview questions & answers

A SDE-2 was asked Q. Given a binary tree where each node contains a digit from 0-9, find the sum of a...read more
A SDE-2 was asked Q. Write a program to perform a preorder traversal of a binary tree without using r...read more
A SDE-2 was asked Q. Write code to find the median of an unsorted array.

Popular interview questions of SDE-2

A SDE-2 was asked Q1. Given a binary tree where each node contains a digit from 0-9, find the sum of a...read more
A SDE-2 was asked Q2. Write a program to perform a preorder traversal of a binary tree without using r...read more
A SDE-2 was asked Q3. Write code to find the median of an unsorted array.
Amazon SDE-2 Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

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

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits