Median of Data Stream Using Two Heaps
📖 Scenario: Imagine you are building a system that receives a continuous stream of numbers, like temperatures from sensors. You want to quickly find the middle value (median) of all numbers received so far at any time.
🎯 Goal: Build a program in Go that uses two heaps (a max heap and a min heap) to efficiently find the median of a stream of numbers as they arrive.
📋 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 heaps while keeping their sizes balanced.
Calculate the median based on the sizes and top elements of the heaps.
Print the median after inserting each number.
💡 Why This Matters
🌍 Real World
Finding the median in real-time data streams like sensor readings, stock prices, or user ratings.
💼 Career
This technique is useful for software engineers working on data processing, real-time analytics, or financial applications.
Progress0 / 4 steps