Median of Data Stream Using Two Heaps
📖 Scenario: Imagine you are building a system that receives numbers one by one, like scores from a game played live. You want to quickly find the middle score (median) at any time without sorting all scores every time a new number arrives.
🎯 Goal: Build a program that keeps track of numbers using two heaps (a max heap and a min heap) to efficiently find the median after each new number is added.
📋 What You'll Learn
Create two heaps: a max heap for the lower half of numbers and a min heap for the upper half.
Add numbers to the correct heap to keep the halves balanced or almost balanced.
Calculate the median based on the sizes and top elements of the two heaps.
Print the median after adding each number.
💡 Why This Matters
🌍 Real World
Real-time systems like live scoreboards, stock price monitoring, or sensor data analysis need quick median calculations.
💼 Career
Understanding heaps and median calculation is useful for software engineers working on streaming data, real-time analytics, and performance-critical applications.
Progress0 / 4 steps