Find Median from Data Stream
Problem Statement
Section titled “Problem Statement”LeetCode #295 - Find Median from Data Stream
Section titled “LeetCode #295 - Find Median from Data Stream”Solve this classic coding interview problem efficiently using multiple approaches.
Examples
Section titled “Examples”| Input | Output | Explanation |
|---|---|---|
[example1] | result1 | Explanation goes here |
[example2] | result2 | Explanation goes here |
Constraints
Section titled “Constraints”- Standard LeetCode constraints
- Refer to the official problem statement for exact details
Real-World Applications
Section titled “Real-World Applications”Complexity Comparison
Section titled “Complexity Comparison”| Approach | Add/Find Median | Space | Best When |
|---|---|---|---|
| Two Heaps | O(log n) / O(1) | O(n) | Optimal for streaming |
| Balanced BST | O(log n) / O(log n) | O(n) | Self-balancing trees |
| Sorted List | O(n) / O(1) | O(n) | Simpler implementation |
Which approach should you learn first?
Section titled “Which approach should you learn first?”- Pick Two Heaps for the optimal streaming solution.
- Pick Balanced BST for a theoretical balanced approach.
- Pick Sorted List if you are learning from scratch.
Optimal
Two Heaps
O(log n) add · O(1) median
Balanced Tree
Balanced BST
O(log n) add · O(log n) median
Simple
Sorted List
O(n) add · O(1) median
Approach 1: Two Heaps (Recommended)
Section titled “Approach 1: Two Heaps (Recommended)”Maintain two heaps: a max heap for the smaller half and a min heap for the larger half. Keep them balanced so the median is either the root of one or the average of both roots.
⏱ Time O(log n) Add/Find 💾 Space O(n) Heap storage
Solution Code
Section titled “Solution Code”from typing import List
def find_median_from_data_stream_two_heaps(nums: List[int]) -> int: """ Find Median From Data Stream - Two Heaps approach.
Time: O(n), Space: O(1) or O(n) depending on approach """ pass
if __name__ == "__main__": # Test cases pass#include <iostream>#include <vector>#include <unordered_map>using namespace std;
class Solution {public: int find_median_from_data_stream_two_heaps(vector<int>& nums) { // Find Median From Data Stream - Two Heaps approach return 0; }};
int main() { return 0;}import java.util.*;
class Solution { public int find_median_from_data_stream_two_heaps(int[] nums) { // Find Median From Data Stream - Two Heaps approach return 0; }
public static void main(String[] args) { // Test cases }}/** * {@param {number[]} nums} * {@return {number}} */var find_median_from_data_streamTwoheaps = function(nums) { // Find Median From Data Stream - Two Heaps approach return 0;};
if (require.main === module) { // Test cases}pub fn find_median_from_data_stream_two_heaps(nums: Vec<i32>) -> i32 { // Find Median From Data Stream - Two Heaps approach 0}
fn main() { // Test cases}package main
import "fmt"
func find_median_from_data_streamTwoheaps(nums []int) int { // Find Median From Data Stream - Two Heaps approach return 0}
func main() { // Test cases}Approach 2: Balanced BST
Section titled “Approach 2: Balanced BST”Use a self-balancing binary search tree to maintain sorted order and compute the median dynamically.
⏱ Time O(log n) Balanced tree ops 💾 Space O(n) Tree storage
Solution Code
Section titled “Solution Code”from typing import List
def find_median_from_data_stream_balanced_bst(nums: List[int]) -> int: """ Find Median From Data Stream - Balanced Bst approach.
Time: O(n), Space: O(1) or O(n) depending on approach """ pass
if __name__ == "__main__": # Test cases pass#include <iostream>#include <vector>#include <unordered_map>using namespace std;
class Solution {public: int find_median_from_data_stream_balanced_bst(vector<int>& nums) { // Find Median From Data Stream - Balanced Bst approach return 0; }};
int main() { return 0;}import java.util.*;
class Solution { public int find_median_from_data_stream_balanced_bst(int[] nums) { // Find Median From Data Stream - Balanced Bst approach return 0; }
public static void main(String[] args) { // Test cases }}/** * {@param {number[]} nums} * {@return {number}} */var find_median_from_data_streamBalancedbst = function(nums) { // Find Median From Data Stream - Balanced Bst approach return 0;};
if (require.main === module) { // Test cases}pub fn find_median_from_data_stream_balanced_bst(nums: Vec<i32>) -> i32 { // Find Median From Data Stream - Balanced Bst approach 0}
fn main() { // Test cases}package main
import "fmt"
func find_median_from_data_streamBalancedbst(nums []int) int { // Find Median From Data Stream - Balanced Bst approach return 0}
func main() { // Test cases}Approach 3: Sorted List
Section titled “Approach 3: Sorted List”Maintain a sorted list and insert each new number in the correct position. The median is found at the middle position.
⏱ Time O(n) Linear insertion 💾 Space O(n) List storage
Solution Code
Section titled “Solution Code”from typing import List
def find_median_from_data_stream_sorted_list(nums: List[int]) -> int: """ Find Median From Data Stream - Sorted List approach.
Time: O(n), Space: O(1) or O(n) depending on approach """ pass
if __name__ == "__main__": # Test cases pass#include <iostream>#include <vector>#include <unordered_map>using namespace std;
class Solution {public: int find_median_from_data_stream_sorted_list(vector<int>& nums) { // Find Median From Data Stream - Sorted List approach return 0; }};
int main() { return 0;}import java.util.*;
class Solution { public int find_median_from_data_stream_sorted_list(int[] nums) { // Find Median From Data Stream - Sorted List approach return 0; }
public static void main(String[] args) { // Test cases }}/** * {@param {number[]} nums} * {@return {number}} */var find_median_from_data_streamSortedlist = function(nums) { // Find Median From Data Stream - Sorted List approach return 0;};
if (require.main === module) { // Test cases}pub fn find_median_from_data_stream_sorted_list(nums: Vec<i32>) -> i32 { // Find Median From Data Stream - Sorted List approach 0}
fn main() { // Test cases}package main
import "fmt"
func find_median_from_data_streamSortedlist(nums []int) int { // Find Median From Data Stream - Sorted List approach return 0}
func main() { // Test cases}