Priority Queue Introduction and Concept in DSA C - Time & Space Complexity
We want to understand how the time to add or remove items from a priority queue changes as we add more items.
How does the work grow when the queue gets bigger?
Analyze the time complexity of the following code snippet.
// Insert an element into a priority queue implemented as a binary heap
void insert(int heap[], int *size, int value) {
int i = (*size)++;
while (i > 0 && value > heap[(i - 1) / 2]) {
heap[i] = heap[(i - 1) / 2];
i = (i - 1) / 2;
}
heap[i] = value;
}
This code adds a new value to a max-heap priority queue by moving it up until the heap property is restored.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The while loop that moves the new value up the heap.
- How many times: It runs at most once per level of the heap, moving from the bottom to the top.
As the number of items (n) grows, the heap gets taller, so the number of steps to move up grows slowly.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 4 steps (height of heap) |
| 100 | About 7 steps |
| 1000 | About 10 steps |
Pattern observation: The steps grow slowly, roughly with the height of the heap, which increases as the logarithm of n.
Time Complexity: O(log n)
This means adding an item takes time that grows slowly as the queue gets bigger, only about the height of the heap.
[X] Wrong: "Adding an item always takes the same time no matter how big the queue is."
[OK] Correct: Because the new item may need to move up many levels to keep the order, so bigger queues can take more steps.
Knowing how priority queues work and their time costs helps you explain efficient ways to manage tasks by importance, a common real-world problem.
"What if we used a simple unsorted array instead of a heap? How would the time complexity of adding and removing items change?"
Practice
priority queue compared to a regular queue?Solution
Step 1: Understand queue behavior
A regular queue removes elements in the order they arrive (FIFO).Step 2: Understand priority queue behavior
A priority queue removes elements based on priority, not arrival order.Final Answer:
Elements are dequeued based on their priority, not just arrival order -> Option DQuick Check:
Priority queue = priority-based removal [OK]
- Thinking priority queue is same as FIFO queue
- Assuming elements must be unique
- Believing insertion order is always preserved
Solution
Step 1: Recall C standard library features
The C standard library does not include a built-in priority queue type.Step 2: Analyze options
The other options show declaration syntax resembling C++ (priority_queue or struct) or Java (PriorityQueue class), none of which are standard in C.Final Answer:
There is no built-in priority queue in standard C -> Option AQuick Check:
C standard library lacks priority queue [OK]
- Confusing C with C++ STL syntax
- Assuming priority queue is built-in in C
- Using Java or C++ syntax in C code
insert(5)
insert(3)
insert(10)
extract_max()
insert(1)
extract_max()
What is the sequence of extracted elements?
Solution
Step 1: Insert elements into max-priority queue
Insert 5, 3, 10. The max element is 10.Step 2: Extract max elements in order
First extract_max() returns 10. Then insert 1. Next extract_max() returns 5 (next highest).Final Answer:
[10, 5] -> Option CQuick Check:
Extracted max elements = [10, 5] [OK]
- Extracting elements in insertion order
- Confusing min and max priority
- Ignoring new insertions before extraction
void insert(int pq[], int *size, int value) {
pq[*size] = value;
(*size)++;
int i = *size - 1;
while (i > 0 && pq[i] < pq[(i - 1) / 2]) {
int temp = pq[i];
pq[i] = pq[(i - 1) / 2];
pq[(i - 1) / 2] = temp;
i = (i - 1) / 2;
}
}What is the bug in this insertion logic?
Solution
Step 1: Understand heap property for max-heap
Max-heap requires parent >= children, so swap if child > parent.Step 2: Analyze condition in while loop
Condition uses pq[i] < pq[parent], which is correct for min-heap, not max-heap.Final Answer:
It uses < instead of > for max-heap property -> Option AQuick Check:
Max-heap needs child > parent to swap [OK]
- Using min-heap condition for max-heap
- Incorrect parent index calculation
- Not incrementing size correctly
Solution
Step 1: Understand requirement for priority and insertion order
We want highest priority first, but for equal priority, process by insertion order.Step 2: Choose data structure supporting priority and tie-break
A max-heap with timestamps can break ties by earlier insertion time, preserving order among equals.Final Answer:
Use a max-heap with a timestamp to break ties -> Option BQuick Check:
Max-heap + timestamp = priority + insertion order [OK]
- Ignoring insertion order for equal priorities
- Using simple array without tie-break
- Using stack or queue ignoring priority
Ready to prove it?
Pass the test and this pattern moves unknown
