Skip to content
playdsa
Preferences

Make yourself comfortable.

Saved on this browser. Your device’s reduced-motion preference is always respected.

Theme
Advanced settings

The Median Balance

Learn
Play
Prove
Problem context and objectives
Mission briefing

Matchmaking coordinator · Urgent players and jobs compete for limited slots

Keep the best next candidate available without sorting everything again: The Median Balance.

A stale priority frontier increases wait time for everyone.

How you win

  1. 1Recognize when Two-heap median matches the clues
  2. 2Keep this true after every move: every lower value is no greater than every upper value and heap sizes differ by at most one
  3. 3Reach the result within O(n log n)

Rules and pressure

  • Target cost: O(n log n)
  • State rule: every lower value is no greater than every upper value and heap sizes differ by at most one
Lesson 1 of 3

Live algorithm trace

Two-heap median

Complete execution
1 of 6
5
next
15
1
3

Initialize the lower max-heap and upper min-heap.

1lower = max heap; upper = min heap
2insert into the matching half
3rebalance if sizes differ by more than one
4if sizes match: average roots
5otherwise use the larger heap root
6record each median
lower = []upper = []
Truth to preserve / Cost target
Truth to preserve

every lower value is no greater than every upper value and heap sizes differ by at most one

Cost target

O(n log n)

A max-heap stores the lower half and a min-heap stores the upper half. Rebalance until their sizes differ by at most one, so the median is always available at their roots.

Your call · What should guide every step of this algorithm?

Help shape PlayDSA

Something confusing, broken, or missing? Leave a quick note without leaving your lesson.

Please leave out passwords, payment details and other private information.

Page included: /

Sign in to save feedback here, or send it with your email app. Your draft stays here while you sign in.

Open email instead