Heaps and Priority Queue Quiz
40 comprehensive questions exploring heap data structures — with 16 code examples covering heap operations, array representation, building heaps, priority queues, heap sort, and advanced variants in this cpp quiz.
Question 1
What is a heap data structure?
Question 2
What is the difference between a min-heap and max-heap?
Min-heap: parent <= children
Max-heap: parent >= childrenQuestion 3
How does heap insert operation work?
Question 4
What is the time complexity of heap insert?
Insert: O(log n) worst case
Bubble up through heightQuestion 5
How does heap extract-min/max work?
Question 6
What is heapify-down operation?
Compare with children
Swap with smallest/largest child
Recur until heap property satisfiedQuestion 7
What is the time complexity of heap extract operation?
Question 8
How are heaps represented in arrays?
Root at index 0
Left child: 2*i + 1
Right child: 2*i + 2
Parent: (i-1)/2Question 9
What is the memory efficiency of array-based heaps?
Question 10
How do you calculate parent index from child index in heap arrays?
Parent of i: (i-1)/2
Integer division for floorQuestion 11
What is the build-heap algorithm?
Question 12
What is the time complexity of build-heap?
Build-heap: O(n)
Better than n log n insertionsQuestion 13
How does top-down heap construction differ from bottom-up?
Question 14
What is a priority queue?
Priority queue: ordered access
Highest/lowest priority firstQuestion 15
How do heaps implement priority queues?
Question 16
What is heap sort algorithm?
Build max-heap
Repeatedly extract max
Place at end of arrayQuestion 17
Why is heap sort O(n log n)?
Question 18
What is the space complexity of heap sort?
In-place sorting
O(1) auxiliary spaceQuestion 19
How does heap sort compare to quicksort?
Question 20
What is a binomial heap?
Collection of binomial trees
Efficient merge operationsQuestion 21
What is a Fibonacci heap?
Question 22
What is a d-ary heap?
Each node has d children
Reduces height for large dQuestion 23
How do heap variants compare in performance?
Question 24
What is heap stability in priority queues?
Stable: equal priorities maintain order
Unstable: order may changeQuestion 25
How do comparators work in heap-based priority queues?
Question 26
What are the applications of priority queues in scheduling?
OS process scheduling
Event simulation
Task managementQuestion 27
How does heap height affect performance?
Question 28
What is the heap property formally?
For max-heap: A[parent] >= A[child]
For min-heap: A[parent] <= A[child]Question 29
How do heaps handle duplicate values?
Question 30
What is the practical branching factor for d-ary heaps?
d typically 3-8
Balances find-min and heightQuestion 31
How do Fibonacci heaps achieve fast decrease-key?
Question 32
What is the amortized analysis of heap operations?
Amortized: average over sequence
Accounts for infrequent expensive operationsQuestion 33
How do heaps support decrease-key operations?
Question 34
What is the cache performance of array-based heaps?
Contiguous memory access
Good spatial localityQuestion 35
How do binomial heaps support fast merging?
Question 36
What is the trade-off in choosing heap variants?
Binary heap: simple, balanced
Advanced: optimized for specific operationsQuestion 37
How do heaps handle dynamic priorities?
Question 38
What is the heap construction bottleneck?
Bottom-up: O(n) optimal
Top-down: O(n log n) suboptimalQuestion 39
How do heaps compare to sorted arrays for priority queues?
Question 40
Considering heap data structures and their implementation challenges, which fundamental property makes heaps essential for efficient priority-based algorithms despite the complexity of maintaining the heap invariant through heapify operations?
