🎙 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 7 of 10

What does this print? (code)

What does this print?

code = {"a": "0", "b": "10", "c": "11"}
reverse = {w: ch for ch, w in code.items()}
out, cur = "", ""
for bit in "10011010":
    cur += bit
    if cur in reverse:
        out += reverse[cur]
        cur = ""
print(out)
  1. A.baca
  2. B.bacab
  3. C.bcab
  4. D.abcab
Answer & explanation

Correct answer: bacab

Split as 10 | 0 | 11 | 0 | 10, which reads b, a, c, a, b. A prefix code never needs to look ahead.

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 →