How Priority Queues (Heaps) Work — API Cheat Sheet
No exercise here — how to get a min-heap or max-heap in Java, C#, Python, and JavaScript/TypeScript, since only two of these languages ship one built in.
This is a reference page, not a graded exercise. A priority queue (backed by a binary heap) always gives you O(log n) access to the current minimum (or maximum) element — reach for it for "top k", "k closest", "merge k sorted lists", and Dijkstra-style problems. It does not keep everything fully sorted (that's a sorted set/tree, see that cheat sheet) — it only guarantees the min/max is cheap to get.
Java — PriorityQueue<T> (min-heap by default)
| Want to... | Method | Example |
|---|---|---|
| Create (min-heap) | new PriorityQueue<>() | smallest element comes out first |
| Create (max-heap) | pass a comparator | new PriorityQueue<>(Comparator.reverseOrder()) |
| Custom ordering | pass a lambda | new PriorityQueue<>((a, b) -> a.dist - b.dist) |
| Insert | .add(v) / .offer(v) | |
| Remove-and-return min/max | .poll() | returns null if empty |
| Peek | .peek() | |
| Size | .size() |
C# — PriorityQueue<TElement, TPriority> (.NET 6+, min-heap by default)
| Want to... | Method | Example |
|---|---|---|
| Create | new PriorityQueue<TElement, TPriority>() | |
| Insert | .Enqueue(item, priority) | separate item and priority, unlike Java |
| Remove-and-return min | .Dequeue() | returns the item, not the priority |
| Peek | .Peek() | |
| Max-heap | negate the priority | .Enqueue(item, -priority), since there's no built-in max mode |
Python — heapq (min-heap only, operates on a plain list)
| Want to... | Method | Example |
|---|---|---|
| Create | just use a list | heap = [] |
| Insert | heapq.heappush(heap, v) | |
| Remove-and-return min | heapq.heappop(heap) | |
| Peek | heap[0] | the min is always at index 0 |
| Max-heap | push negated values | heapq.heappush(heap, -v), negate again on pop |
| Custom ordering | push tuples | heapq.heappush(heap, (priority, item)) — compares tuples element-by-element, so ties fall back to comparing item |
| Build from an existing list | heapq.heapify(lst) | O(n), turns lst into a heap in place |
JavaScript / TypeScript — no built-in, you write it or import one
Neither language ships a heap/priority queue. Options, roughly in order of what's acceptable in an interview:
| Approach | Notes |
|---|---|
| Say so, and describe the binary-heap algorithm | often enough — interviewers frequently just want to hear you know what a heap is and how sift-up/sift-down work |
Use a small library (e.g. a MinHeap from a project's utils) | fine if the interview allows external code/libraries |
| Fake it with a sorted array | arr.push(v) then re-sort, or binary-search-insert — O(n log n) per insert instead of O(log n), acceptable for small inputs if you name the tradeoff |
The gotcha that costs the most time: every one of these is a min-heap by default — if the problem wants the largest elements out first (top-k largest, for instance), you must explicitly flip it: a comparator in Java, a negated priority in C#, negated values in Python. Forgetting this is the single most common priority-queue bug in interviews.
Hints
Companies
No companies reported yet.
Discussion
Sign in to join the discussion.
Loading discussion...
Test results
This question doesn't have a code exercise.