← Quiz: Heaps and Heap Sort Internals: all 15 questions
Quiz: Heaps and Heap Sort Internals · Question 7 of 15
What does this print? (code)
What does this print?
import heapq
h = []
for x in [4, 1, 7, 3]:
heapq.heappush(h, -x)
print(-heapq.heappop(h), -h[0])
- A.1 3
- B.7 1
- C.4 3
- D.7 4
Answer & explanation
Correct answer: 7 4
Negating turns heapq's min-heap into a max-heap: the first pop gives 7, and the next largest, 4, is now on top.
Can you get the rest right without peeking?
Take the timed Quiz: Heaps and Heap Sort Internals quiz free. Pass it to earn XP and count towards your certificate.
Start the quiz →