← Heaps & Priority Queues Quiz: all 8 questions
Heaps & Priority Queues Quiz · Question 4 of 8
How do you get max-heap behaviour with Python's heapq?
How do you get max-heap behaviour with Python's heapq?
- A.heapq.maxheap()
- B.Push negated values and negate again when popping
- C.Sort in reverse after each push
- D.It's not possible
Answer & explanation
Correct answer: Push negated values and negate again when popping
heapq only implements a min-heap; storing −x turns the largest x into the smallest key.
Can you get the rest right without peeking?
Take the timed Heaps & Priority Queues Quiz quiz free. Pass it to earn XP and count towards your certificate.
Start the quiz →