🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101
← 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?

  1. A.heapq.maxheap()
  2. B.Push negated values and negate again when popping
  3. C.Sort in reverse after each push
  4. 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 →