Heap pattern
Median from Data Stream
Design a data structure that supports adding integers from a stream and returning the median of all values seen so far at any time. The median is the middle value, or the average of the two middle values when the count is even. Watch it run step by step, then write your own pseudocode and get it checked by AI.
- LeetCode 295
- Hard
- 2 approaches
Sign in with Google or GitHub and you land straight on this page. DSA comes with the all labs plan and every ChaiCode batch.
HeapLeetCode 295
Hard
From the first idea to the best one
- 01 Brute force · keep a sorted list O(n²)O(n) space
- 02 Optimized · max-heap + min-heap O(log n) addO(n) space
What you will learn
- How to read Median from Data Stream and spot the heap pattern in it
- Brute force · keep a sorted list: O(n²) time, O(n) space
- Optimized · max-heap + min-heap: O(log n) add time, O(n) space
- The pseudocode for each approach, its line lit as the animation runs
- Practice: write your own pseudocode and get it checked
More Heap
See Median from Data Stream run, step by step
Sign in and this page opens on the animation, with all 7 Heap problems beside it.
DSA comes with the all labs plan and every ChaiCode batch.