🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101
← Quiz: Rooted Trees and Huffman Coding: all 10 questions

Quiz: Rooted Trees and Huffman Coding · Question 10 of 10

What does this print? (code)

What does this print?

import heapq
heap = [5, 2, 2, 1, 1]
heapq.heapify(heap)
total = 0
while len(heap) > 1:
    s = heapq.heappop(heap) + heapq.heappop(heap)
    total += s
    heapq.heappush(heap, s)
print(total)
  1. A.11
  2. B.33
  3. C.23
  4. D.21
Answer & explanation

Correct answer: 23

These are the abracadabra frequencies; the internal weights 2 + 4 + 6 + 11 add up to 23 bits.

Can you get the rest right without peeking?

Take the timed Quiz: Rooted Trees and Huffman Coding quiz free. Pass it to earn XP and count towards your certificate.

Start the quiz →