CoursesSearch & Priority Patterns

Heap & Top-K

Lessons
9
Exercises
56
Minutes
55
  1. 1

    The Priority Queue

    What a heap actually guarantees — and why keeping only partial order makes it fast.

    Multiple choiceTap
    5 exercises
    4 min
  2. 2

    Kth Largest in a Stream

    Cap a min-heap at k elements — its top is always the kth largest, no matter how much streams past.

    Complete codeDebugMultiple choiceTrace
    6 exercises
    6 min
  3. 3

    Last Stone Weight

    Simulate with a max-heap: pop the two heaviest, push back the difference — never re-sort.

    DebugCode orderMultiple choiceTrace
    6 exercises
    5 min
  4. 4

    Kth Largest in an Array

    Keep only the k largest in a min-heap — pay log k per element, not log n.

    Complete codeMultiple choiceTrace
    6 exercises
    6 min
  5. 5

    Top K Frequent Elements

    Count first, then heap the pairs — and order the heap by COUNT, never by value.

    Complete codeMultiple choiceTrace
    6 exercises
    6 min
  6. 6

    K Closest Points to Origin

    Keep the k closest by evicting the farthest — a MAX-heap, the mirror image of top-K largest.

    Complete codeDebugMultiple choiceTrace
    6 exercises
    6 min
  7. 7

    Heap & Top-K CheckpointCheckpoint

    No new concepts — combine the island's heap moves: pick the right heap kind, run the size-k eviction, and catch the classic wrong-heap bug.

    DebugCode orderFill blankMultiple choiceTrace
    6 exercises
    6 min
  8. 8

    Heap & Top-K ReviewReview

    Mixed recap of the island: what a heap actually promises, size-k eviction, max-heap simulation, 'worst on top' comparators, and the cost of top-K.

    Complete codeDebugMultiple choiceTapTrace
    7 exercises
    6 min
  9. 9

    Boss: Median from a StreamBoss

    Two heaps split the stream down the middle — a max-heap holds the smaller half, a min-heap the larger — so both middle values wait at the tops.

    Complete codeDebugCode orderMultiple choiceTrace
    8 exercises
    10 min
AlgoFox Pro

Search & Priority Patterns is part of AlgoFox Pro

You can read the whole course outline here. The exercises are in the app, along with your streak, your mistake review, and the progress that carries across your devices.

Pro starts with a 3 day free trial. The app store decides who is eligible for an introductory offer, and a lapsed subscription does not get another one.

Download AlgoFox on the App StoreGet AlgoFox on Google Play

Already subscribed in the app? Sign in with the same account and Pro works here too. Quick Play and Battle are free on the web, and so is the first course in every subject.