Skip to interview prep
Apexed

48. How does Huffman coding work?

HardGreedy algorithms

Technical

Type or dictate your answer. Your draft is saved on this device.

Show model answer

Count character frequencies, then repeatedly merge the two least frequent nodes (using a min-heap) into a new node whose weight is their sum. The resulting tree gives each character a prefix-free binary code, with frequent characters getting shorter codes; this is optimal among prefix codes.

Example program

How does Huffman coding work — Data structures & algorithms interview question — Apexed