Data Structures & AlgorithmsHardTCS NQT · greedy coding

Problem: Which property must hold for Huffman coding to produce an optimal prefix code?

  1. A. All frequencies must be equal
  2. B. Merging the two least-frequent symbols first is always part of some optimal tree (exchange argument)
  3. C. The alphabet must be binary numbers
  4. D. The code must be fixed-length

Correct answer: B. Merging the two least-frequent symbols first is always part of some optimal tree (exchange argument)

An exchange argument shows that the two rarest symbols can be placed as siblings at the deepest level of an optimal tree.

More Coding Interview Problems questions

Exams that ask this topic