← Quiz: Heaps and Heap Sort Internals: all 15 questions
Quiz: Heaps and Heap Sort Internals · Question 11 of 15
What does this print? (code)
What does this print?
import heapq
h = []
for x in [5, 3, 8, 1, 9, 2]:
heapq.heappush(h, x)
print(h[0], [heapq.heappop(h) for _ in range(3)])
- A.5 [1, 2, 3]
- B.1 [9, 8, 5]
- C.1 [1, 2, 3]
- D.1 [1, 3, 2]
Answer & explanation
Correct answer: 1 [1, 2, 3]
heapq is a min-heap: h[0] is always the smallest, and each pop returns the next smallest.
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 →