🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101
← 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])
  1. A.1 3
  2. B.7 1
  3. C.4 3
  4. 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 →