The Find Median from Data Stream interview question asks you to design a data structure that supports two operations:
addNum(num): Adds an integer from the data stream to the collection.findMedian(): Returns the median of all elements seen so far.
The median is the middle value in a sorted list. If the list has an even number of elements, the median is the average of the two middle values.This is one of the most famous "Hard" design questions, asked by almost every big tech company (Apple, Uber, Amazon, Google). It tests your ability to maintain sorted order in a dynamic dataset. A naive solution (sorting every time) is . The challenge is to achieve for adding and for finding the median using the Heap interview pattern.
The problem is solved using Two Heaps (Dual Heap).
Stream: 1, 3, 2.
sort() on a list for every new number ().The "Two Heaps" pattern is the standard solution for any problem involving a "sliding" or "dynamic" median. Practice explaining the balancing step, as it's the most critical part of the logic.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Design a File Sharing System | Medium | Solve | |
| Design Search Autocomplete System | Hard | Solve | |
| Sequentially Ordinal Rank Tracker | Hard | Solve | |
| Finding MK Average | Hard | Solve | |
| Stock Price Fluctuation | Medium | Solve |