AlgoViz

Two heaps

medium 22 min

The two heaps pattern keeps a max-heap for the smaller half of the values and a min-heap for the larger half.

Where it shows up

The same pattern appears across everyday software.

  • Live dashboards and monitoring

    Showing the median response time or a p90 latency over a recent window of requests. Small services can track it exactly with two heaps; very large ones switch to approximate sketches such as t-digest.

  • Median filters for sensors and images

    A sliding window median removes spikes from sensor readings and salt-and-pepper noise from images while keeping edges sharp, which a moving average cannot do.

  • Trading and pricing feeds

    A running median price over the last N trades resists single outlier prints far better than a mean, so it is a common reference value in market data tools.

  • Capital and resource allocation

    Choosing the most profitable job you can afford right now, where finished jobs unlock bigger ones: the IPO pattern with a min-heap on cost and a max-heap on payoff.

  • Room and machine scheduling

    Allocators keep free resources in one heap (lowest id first) and busy ones in another (earliest finish first), moving items between them as time advances.