easyHeap Priority Queue 0 views

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...MethodExample
Create (min-heap)new PriorityQueue<>()smallest element comes out first
Create (max-heap)pass a comparatornew PriorityQueue<>(Comparator.reverseOrder())
Custom orderingpass a lambdanew 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...MethodExample
Createnew 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-heapnegate 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...MethodExample
Createjust use a listheap = []
Insertheapq.heappush(heap, v)
Remove-and-return minheapq.heappop(heap)
Peekheap[0]the min is always at index 0
Max-heappush negated valuesheapq.heappush(heap, -v), negate again on pop
Custom orderingpush tuplesheapq.heappush(heap, (priority, item)) — compares tuples element-by-element, so ties fall back to comparing item
Build from an existing listheapq.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:

ApproachNotes
Say so, and describe the binary-heap algorithmoften 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 arrayarr.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.