โ† Back to the queues page

Priority Queue

Not FIFO at all: the highest-priority element leaves first, regardless of arrival. Almost always implemented as a binary heap.

What to know

  • The queue interface hides a heap: insert and extract are O(log n).
  • Dijkstra, Prim, A*, Huffman, and k-way merge are all priority-queue algorithms.
  • See the Heaps deep dive for the mechanics of the underlying structure.

In the wild: OS process schedulers, hospital triage, event-driven simulators, job queues with priorities.

Algorithms to reach for

Dijkstra with PQ

O((V+E) log V)

Always expand the closest unsettled node

K-way merge

O(N log k)

Merge k sorted lists by popping the smallest head

Huffman coding

O(n log n)

Merge two rarest symbols repeatedly

Practice problems โ€” with full guides

Each problem has its own page: progressive hints, how to approach it, and the full solution.