Data Structures & AlgorithmsVery hardProduct Company SDE · GATE CS

Problem: Huffman coding is applied to the frequencies 5, 9, 12, 13, 16, 45 by repeatedly merging the two smallest values (the merged value is their sum). What is the total of all merge costs?

  1. A. 124
  2. B. 234
  3. C. 100
  4. D. 224

Correct answer: D. 224

Merges cost 5 + 9 = 14, 12 + 13 = 25, 14 + 16 = 30, 25 + 30 = 55 and 45 + 55 = 100, totalling 224. Forgetting the final merge gives 124, merging left to right without a heap gives 234, and 100 is just the total frequency.

More Coding Interview Problems questions

Exams that ask this topic