Tree Heaps and Priority Queues Quiz
40 comprehensive questions exploring heap data structures — with 16 code examples covering min-heap, max-heap, heapify operations, and priority queue implementations in this cpp quiz.
Question 1
What is a heap data structure?
Question 2
In a max-heap, where is the largest element located?
Max-heap property: parent >= children
Largest: always at rootQuestion 3
What is the time complexity of heap insertion?
Question 4
How does heapify maintain the heap property?
Heapify compares node with children:
If violation, swap and recurseQuestion 5
What is the difference between min-heap and max-heap?
Question 6
In heap deletion (extract-min/max), what happens after removing the root?
Extract root: move last element to root
Then heapify down to maintain propertyQuestion 7
What is heapify-up used for in heap operations?
Question 8
How is a heap typically implemented?
Heap as array:
Parent: i
Left: 2*i+1
Right: 2*i+2Question 9
What is the worst-case time complexity for building a heap?
Question 10
In a priority queue implemented with a heap, what operation has the highest priority?
Priority queue operations:
- Insert: add element
- Extract: remove highest priorityQuestion 11
What is the heap property?
Question 12
How does heap sort work?
Heap sort: build max-heap, then repeatedly
extract max and place at endQuestion 13
What is the space complexity of heap operations?
Question 14
In heap applications, when would you use a min-heap over a max-heap?
Min-heap for:
- Priority queues needing smallest first
- Dijkstra's algorithmQuestion 15
What is the relationship between heap height and performance?
Question 16
How does priority queue insertion work in a heap?
void insert(vector<int>& heap, int val) {
heap.push_back(val);
int i = heap.size() - 1;
while (i > 0 && heap[(i-1)/2] < heap[i]) {
swap(heap[(i-1)/2], heap[i]);
i = (i-1)/2;
}
}Question 17
What makes heaps suitable for priority queues?
Question 18
In heapify-down, what is the process for maintaining heap property?
Heapify down: find largest child
If larger than current, swap and continueQuestion 19
What is the advantage of heap over sorted array for priority queues?
Question 20
How does heap support decrease-key operations?
Decrease key: update value, heapify up
Since value decreased, may need to bubble upQuestion 21
What is the heap condition violated in this structure?
5
/ \
7 3
Max-heap: 7 > 5 violates parent >= childQuestion 22
In which algorithm is heap commonly used?
Question 23
What is the complete binary tree property in heaps?
Complete tree: all levels filled except last
Last level filled left to rightQuestion 24
How does heap support k-way merge operations?
Question 25
What is the amortized analysis of heap operations?
Each operation O(log n)
But heapify-up/down paths overlapQuestion 26
In priority queue applications, when is a max-heap preferred?
Question 27
What is the heap's role in Prim's algorithm?
Prim's: grow MST by adding minimum edge
Heap extracts next closest vertexQuestion 28
How does heap handle duplicate priorities?
Question 29
What is the cache performance of heap operations?
Array-based heap: good locality
Operations access nearby elementsQuestion 30
In heap construction, why is bottom-up heapify O(n)?
Question 31
What is the significance of heaps in algorithm design?
Heaps enable:
- Priority queues O(log n)
- Heap sort O(n log n)
- Graph algorithmsQuestion 32
How does heap differ from binary search tree for priority queues?
Question 33
What is the heap's role in median maintenance algorithms?
Two heaps: max-heap for lower half
Min-heap for upper halfQuestion 34
How does heap support sliding window maximum problems?
Question 35
What is the heap property in terms of array indices?
For max-heap: heap[i] >= heap[2*i+1] && heap[i] >= heap[2*i+2]Question 36
In heap applications, what is the trade-off with binary search trees?
Question 37
How does heap support multi-level priority systems?
Priority as pair: (level, value)
Heap compares first by level, then valueQuestion 38
What makes heap suitable for event-driven simulation?
Question 39
In heap construction, what is the advantage of Floyd's algorithm?
Floyd's: start from bottom, heapify each subtree
O(n) time, better than repeated insertionsQuestion 40
Considering heap operations and their applications in algorithm design, which fundamental property makes heaps essential for efficient priority-based computations despite their structural simplicity?
