Challenge - 5 Problems
Priority Queue Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this priority queue insertion sequence?
Consider a min-priority queue implemented using a heap. Insert the elements 5, 3, 6, 2 in this order. What is the state of the heap array after all insertions?
DSA Python
import heapq pq = [] heapq.heappush(pq, 5) heapq.heappush(pq, 3) heapq.heappush(pq, 6) heapq.heappush(pq, 2) print(pq)
Attempts:
2 left
💡 Hint
Remember that heapq in Python maintains the smallest element at index 0.
✗ Incorrect
The heap after inserting 5, 3, 6, 2 will reorder to keep the smallest element at the root. The final heap array is [2, 3, 6, 5].
🧠 Conceptual
intermediate1:30remaining
Which operation does NOT belong to a priority queue?
Select the operation that is NOT typically supported by a priority queue.
Attempts:
2 left
💡 Hint
Priority queues focus on priority-based access, not searching by value.
✗ Incorrect
Priority queues do not support searching for elements by value efficiently; they focus on insertion and extracting the highest priority element.
🔧 Debug
advanced1:30remaining
What error does this priority queue code raise?
Identify the error raised by this code snippet using Python's heapq module.
DSA Python
import heapq pq = [] heapq.heappush(pq, 10) heapq.heappop(pq) heapq.heappop(pq)
Attempts:
2 left
💡 Hint
Consider what happens when popping from an empty heap.
✗ Incorrect
After popping the only element, the next pop tries to remove from an empty list, causing IndexError.
🚀 Application
advanced2:00remaining
What is the output after these priority queue operations?
Given a max-priority queue implemented by negating values in a min-heap, what is the output after these operations?
Operations:
Insert 4, Insert 7, Insert 1, Extract max, Insert 5
Print the remaining heap array (negated back to positive values).
DSA Python
import heapq pq = [] heapq.heappush(pq, -4) heapq.heappush(pq, -7) heapq.heappush(pq, -1) max_val = -heapq.heappop(pq) heapq.heappush(pq, -5) result = [-x for x in pq] print(sorted(result))
Attempts:
2 left
💡 Hint
Remember to negate values back to positive and sort to visualize the heap contents.
✗ Incorrect
After extracting max (7), the heap contains 4, 1, and after inserting 5, the heap has 1, 4, 5.
🧠 Conceptual
expert1:30remaining
Which data structure property is essential for a priority queue implemented as a heap?
Select the property that must always hold true in a heap-based priority queue.
Attempts:
2 left
💡 Hint
Heaps are stored as arrays and must be complete binary trees.
✗ Incorrect
A heap must be a complete binary tree to maintain efficient insertion and extraction operations.