Compression Algorithm - Problem

Implement a complete Huffman encoding algorithm for text compression and decompression. Your solution should build a Huffman tree from character frequencies, generate unique binary codes for each character, compress the input text, and then decompress it back to verify correctness.

The algorithm should:

  • Count character frequencies in the input text
  • Build a Huffman tree using a priority queue (min-heap)
  • Generate binary codes for each character
  • Compress the text using these codes
  • Decompress the compressed data back to original text
  • Calculate and return the compression ratio

Return a JSON object with: compressedBits (compressed binary string), decompressed (original text), compressionRatio (original bits / compressed bits), and codes (character to binary code mapping).

Input & Output

Example 1 — Basic Compression
$ Input: text = "AAAABBC"
Output: {"compressedBits":"00001010111","decompressed":"AAAABBC","compressionRatio":5.09,"codes":{"A":"0","B":"10","C":"11"}}
💡 Note: A appears 4 times (most frequent) gets code '0', B appears 2 times gets '10', C appears 1 time gets '11'. Compression: A(0)+A(0)+A(0)+A(0)+B(10)+B(10)+C(11) = '00001010111'
Example 2 — Equal Frequencies
$ Input: text = "AABB"
Output: {"compressedBits":"0011","decompressed":"AABB","compressionRatio":4.0,"codes":{"A":"0","B":"1"}}
💡 Note: A and B have equal frequency (2 each), so they get 1-bit codes. Result: A(0)+A(0)+B(1)+B(1) = '0011'
Example 3 — Single Character
$ Input: text = "AAAA"
Output: {"compressedBits":"0000","decompressed":"AAAA","compressionRatio":8.0,"codes":{"A":"0"}}
💡 Note: Single character case: A gets code '0'. Each A becomes '0', so 'AAAA' becomes '0000'. Excellent compression ratio.

Constraints

  • 0 ≤ text.length ≤ 1000
  • text contains only printable ASCII characters
  • Empty string is valid input

Visualization

Tap to expand
INPUT TEXTHUFFMAN ALGORITHMCOMPRESSED RESULTOriginal Text:AAAABBCCharacter Frequencies:A: 4 times (57%)B: 2 times (29%)C: 1 time (14%)Total: 7 characters1Build Min-Heap2Merge Two Smallest3Repeat Until Root4Generate Codes7A:43B:2C:10101Optimal Codes:A: 0 (1 bit)B: 10 (2 bits)C: 11 (2 bits)Compressed Bits:00001010111Compression Stats:Original: 56 bitsCompressed: 11 bitsRatio: 5.09:1Key Insight:A binary tree built by merging the two least frequent nodes createsmathematically optimal codes - frequent characters automatically get shorter paths!TutorialsPoint - Huffman Encoding Compression Algorithm | Optimal Tree-Based Approach
Asked in
Google 35 Amazon 28 Microsoft 22 Facebook 18 Netflix 15
23.4K Views
Medium Frequency
~45 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen