Problem
The median is the middle value of a sorted list of numbers. If the list has an even number of items, the median is the mean of the two middle values.
Design a class that gets a stream of integers and finds the median at any time. A stream is a sequence of numbers that arrive one at a time.
Write the class MedianFinder with these operations:
MedianFinder() creates an empty object. addNum(num) adds the integer num to the data. findMedian() returns the median of all the integers so far. The checker accepts an answer within 10^-5 of the correct value.
Example 1
- Input:
- ["MedianFinder","addNum","addNum","findMedian","addNum","findMedian"] [[],[4],[9],[],[1],[]]
- Output:
- [null, null, null, 6.5, null, 4.0]
- Explanation:
- After 4 and 9, the median is the mean of 4 and 9, which is 6.5. After 1, the sorted data is
[1, 4, 9], and the median is 4.
Example 2
- Input:
- ["MedianFinder","addNum","findMedian","addNum","findMedian","addNum","addNum","findMedian"] [[],[-3],[],[-3],[],[10],[0],[]]
- Output:
- [null, null, -3.0, null, -3.0, null, null, -1.5]
- Explanation:
- At the end, the sorted data is
[-3, -3, 0, 10]. The median is the mean of -3 and 0, which is -1.5.
Constraints
- -10^5 <= num <= 10^5
- The data has at least one integer before each call to findMedian.
- At most 5 * 10^4 calls to addNum and findMedian
Follow-up
How can you make the solution faster if all the integers are between 0 and 100?
SysRound wrote this statement. A reference solution checks every example.
Related questions
Continue with questions that share topics or the same question type.
- Design Search Autocomplete System
Coding · Data Stream · Depth-First Search · Design
- Maximum Number of Points From Grid Queries
Coding · Array · Breadth-First Search · Heap (Priority Queue)
- Meeting Rooms II
Coding · Array · Greedy · Heap (Priority Queue)
- Moving Average from Data Stream
Coding · Array · Data Stream · Design
Sources · 2 references
These records explain where the question appears. A company tag or candidate report does not guarantee a future interview question.
Hello Interview · 3 records
Amazon
Leetcode 295. Find Median from Data Stream
Reported Aug 4, 2026 · 2 source reports
MID_LEVEL, SENIOR
Source snapshot: Nov 12, 2026
View sourceMeta
Leetcode 295. Find Median from Data Stream
Reported Dec 4, 2025 · 3 source reports
JUNIOR, MANAGER, STAFF
Source snapshot: Nov 12, 2026
View sourceMicrosoft
Leetcode 295. Find Median from Data Stream
Reported Feb 26, 2026 · 1 source report
SENIOR
Source snapshot: Nov 12, 2026
View source
LeetCode company tags · 9 records
Adobe
Find Median from Data Stream