Model answer
C++ priority_queue<int> is a max-priority queue. A min-priority queue is priority_queue<int, vector<int>, greater<int>>; both expose top in O(1) and push/pop in O(log n).
Use the key terms, then explain the reasoning.
A priority queue returns the highest- or lowest-priority item rather than the oldest item, commonly using a heap.
A priority queue is an abstract data type; a heap is its common implementation. Items leave according to priority, with arrival order relevant only if the design explicitly includes a stable tie-breaker.
std::priority_queue exposes the largest item under its comparator by default. Custom records require a comparator that establishes strict weak ordering and often include sequence numbers for deterministic ties.
| Operation | Best | Average | Worst | Extra space |
|---|---|---|---|---|
| top | O(1) | O(1) | O(1) | O(1) |
| push | O(log n) | O(log n) | O(log n) | O(1) |
| pop | O(log n) | O(log n) | O(log n) | O(1) |
The comparator states which element has lower priority; greater<int> therefore exposes the minimum.
Open a prompt when you are ready to check your answer.