We use cookies for site analytics. Accept to help us understand how the site is used. See our Privacy Policy for details.
A hard heap / priority queue problem, graded against 6 test cases (3 of them hidden).
A priority queue for "top k" and streaming-median questions.
Reach for it when you see: "Top/smallest k", a running median, or repeatedly needing the current minimum.
More Heap / Priority Queueproblems →Use a max-heap for the lower half and a min-heap for the upper half. When adding a number, place it in the correct heap and rebalance so sizes differ by at most 1. The median is the top of the larger heap, or the average of both tops if equal size.
The full reference solution in every supported language stays in the editor above - reveal it there once you have had a real attempt.
Read off this problem's own test suite, so these are the cases a submission actually has to survive.
These apply to the pattern as a whole, not just this problem.