PracHub
QuestionsLearningGuidesInterview Prep
|Home/Coding & Algorithms/Amazon

Compute the Median After Each Stream Insertion

Last updated: Jul 22, 2026

Quick Overview

Compute the median after every insertion into an integer stream without repeatedly sorting the full prefix. Practice maintaining balanced data structures, handling duplicates and negatives, producing even-count averages safely, and meeting large-input time bounds.

  • medium
  • Amazon
  • Coding & Algorithms
  • Software Engineer

Compute the Median After Each Stream Insertion

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

# Compute the Median After Each Stream Insertion Given a stream of integers, return the median after each value is inserted. For an odd number of observed values, the median is the middle value in sorted order. For an even number, it is the arithmetic mean of the two middle values. ## Function Signature ```python def running_medians(values: list[int]) -> list[float]: ``` ## Input - `values` lists stream arrivals in order. ## Output Return an array of the same length. Element `i` is the median of `values[0:i+1]` and must be represented as a floating-point number. ## Constraints - `0 <= len(values) <= 200_000` - `-10^9 <= values[i] <= 10^9` - Duplicate values are allowed. - The input array must not be mutated. - Compute even-length medians without overflowing a fixed-width integer before conversion. ## Examples ```text values = [5, 2, 10, 4] output = [5.0, 3.5, 5.0, 4.5] ``` ```text values = [-1, -1, 8] output = [-1.0, -1.0, -1.0] ``` ```text values = [] output = [] ```

Quick Answer: Compute the median after every insertion into an integer stream without repeatedly sorting the full prefix. Practice maintaining balanced data structures, handling duplicates and negatives, producing even-count averages safely, and meeting large-input time bounds.

Related Interview Questions

  • Compute Edit Distance - Amazon (medium)
  • Minimize Replacements So Equal Product Values Are Contiguous - Amazon (hard)
  • Find a Maximum-Sum Window in a Sparse Array - Amazon (hard)
  • Add One Source to Minimize Grid Inconvenience - Amazon (medium)
  • Implement a Half-Open Interval Range Module - Amazon (hard)
|Home/Coding & Algorithms/Amazon

Compute the Median After Each Stream Insertion

Amazon logo
Amazon
May 29, 2026, 12:00 AM
mediumSoftware EngineerOnsiteCoding & Algorithms
0
0

Compute the Median After Each Stream Insertion

Given a stream of integers, return the median after each value is inserted. For an odd number of observed values, the median is the middle value in sorted order. For an even number, it is the arithmetic mean of the two middle values.

Function Signature

def running_medians(values: list[int]) -> list[float]:

Input

  • values lists stream arrivals in order.

Output

Return an array of the same length. Element i is the median of values[0:i+1] and must be represented as a floating-point number.

Constraints

  • 0 <= len(values) <= 200_000
  • -10^9 <= values[i] <= 10^9
  • Duplicate values are allowed.
  • The input array must not be mutated.
  • Compute even-length medians without overflowing a fixed-width integer before conversion.

Examples

values = [5, 2, 10, 4]
output = [5.0, 3.5, 5.0, 4.5]
values = [-1, -1, 8]
output = [-1.0, -1.0, -1.0]
values = []
output = []

Submit Your Answer to Earn 20XP

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More Amazon•More Software Engineer•Amazon Software Engineer•Amazon Coding & Algorithms•Software Engineer Coding & Algorithms
PracHub

Master your tech interviews with 8,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • AI Coding Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.