What is a priority queue?

Why Interviewers Ask This

Interviewers use this question to quickly assess whether a candidate has the foundational knowledge required for Data Structures & Algorithms development. It reveals whether you understand the building blocks that more complex concepts rely on.

Answer

A priority queue is an abstract data type where each element has a priority, and elements are dequeued in priority order (not FIFO). The element with the highest (or lowest) priority is always dequeued first. Implementation: almost always backed by a binary heap. Operations: insert(element, priority): O(log n); extractMax/extractMin: O(log n); peek/top: O(1); decreaseKey/increaseKey: O(log n) — used in graph algorithms; buildHeap: O(n). Min-priority queue returns the element with the smallest priority (key) first. Max-priority queue returns the largest. Language implementations: Java: PriorityQueue (min-heap by default); Python: heapq (min-heap); C++: priority_queue (max-heap by default). Applications: (1) Dijkstra's shortest path — always process the unvisited node with smallest tentative distance; (2) Prim's MST; (3) Huffman encoding; (4) Task scheduling (process highest-priority jobs first); (5) Top K elements — maintain a heap of size K; (6) K-way merge of sorted lists; (7) A* pathfinding.

Pro Tip

Demonstrate both theoretical understanding and practical experience. Say what it is, then give an example of how you actually used it in a Data Structures & Algorithms codebase.