We use cookies for site analytics. Accept to help us understand how the site is used. See our Privacy Policy for details.
When you need the largest or smallest element repeatedly but do not need everything sorted, a heap gives you O(log n) per operation instead of a full O(n log n) sort each time. Top-k is the canonical use; the two-heap trick for a streaming median is the one that separates candidates. Remember that a size-k heap solves top-k in O(n log k), which is the answer interviewers are usually fishing for.
"Top/smallest k", a running median, or repeatedly needing the current minimum.
10 problems. Each one runs in an in-browser editor against real test cases, with hints and a worked solution.
The patterns cheat sheet covers all of these in one page - what each one looks like in a problem statement, and the template that follows.