Data Structures & AlgorithmsHardProduct-company MCQ pattern

Characters a, b, c, d, e occur 4, 7, 9, 12 and 20 times. With an optimal Huffman code, how many bits are needed to encode the whole text?

  1. A. 120
  2. B. 115
  3. C. 116
  4. D. 156

Correct answer: B. 115

Merges: 4 + 7 = 11; 9 + 11 = 20; 12 + 20 = 32; 20 + 32 = 52. One optimal set of code lengths is a 4, b 4, c 3, d 2, e 1 (the tie between the two 20s can give another set with the same total), so 4·4 + 7·4 + 9·3 + 12·2 + 20·1 = 115 (also the sum of the merged weights, 11 + 20 + 32 + 52). A fixed 3-bit code would need 52 × 3 = 156.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic