Data Structures & AlgorithmsHardCAT · GATE CS

Problem: Huffman coding builds an optimal prefix-free binary code for a set of symbol frequencies. Which greedy step does it repeat?

  1. A. Assign every symbol a fixed-length code
  2. B. Repeatedly split the highest-frequency node in half
  3. C. Repeatedly merge the two lowest-frequency nodes (using a min-heap) into a new combined node
  4. D. Sort all symbols alphabetically and assign codes in that order

Correct answer: C. Repeatedly merge the two lowest-frequency nodes (using a min-heap) into a new combined node

A min-heap always exposes the two least-frequent nodes; merging them repeatedly (building the tree bottom-up) is proven optimal for minimizing expected code length, running in O(n log n).

More Coding Interview Problems questions

Exams that ask this topic