Challenge - 5 Problems
Huffman Encoding Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Huffman Encoding Tree Construction
What is the output of the following code that builds a Huffman tree and prints the codes for each character?
DSA C
typedef struct Node {
char ch;
int freq;
struct Node *left, *right;
} Node;
// Assume a min-heap and tree building functions are implemented correctly
// Characters and frequencies
char chars[] = {'a', 'b', 'c', 'd', 'e', 'f'};
int freq[] = {5, 9, 12, 13, 16, 45};
// After building Huffman tree, codes are printed
// Output format: character: code
// For example: a: 110
// The expected codes are:
// f: 0
// c: 100
// d: 101
// a: 1100
// b: 1101
// e: 111
// The printed output is:
// f: 0
// c: 100
// d: 101
// a: 1100
// b: 1101
// e: 111Attempts:
2 left
💡 Hint
Recall that characters with higher frequency get shorter codes in Huffman encoding.
✗ Incorrect
The Huffman tree built from the given frequencies assigns the shortest code to 'f' (highest frequency) and longer codes to less frequent characters. Option D matches the correct Huffman codes.
🧠 Conceptual
intermediate1:30remaining
Minimum Number of Bits in Huffman Encoding
Given characters with frequencies: {'x': 10, 'y': 15, 'z': 30}, what is the minimum total number of bits needed to encode the string "xyzxyz" using Huffman encoding?
Attempts:
2 left
💡 Hint
Calculate the Huffman codes for each character and multiply by their occurrences in the string.
✗ Incorrect
The Huffman codes assign shorter codes to higher frequency characters. For "xyzxyz", total bits = sum of (code length * frequency in string). Option C matches the correct calculation.
🔧 Debug
advanced2:00remaining
Identify the Error in Huffman Tree Node Comparison
What error does the following C code snippet cause when used in building a Huffman tree?
typedef struct Node {
char ch;
int freq;
struct Node *left, *right;
} Node;
int compare(const void *a, const void *b) {
Node *nodeA = (Node *)a;
Node *nodeB = (Node *)b;
return nodeA->freq - nodeB->freq;
}
// Used in qsort to sort array of Node pointers
Attempts:
2 left
💡 Hint
Consider what qsort passes to the compare function when sorting pointers.
✗ Incorrect
qsort passes pointers to elements, so when sorting an array of Node pointers, the compare function receives pointers to pointers. The code casts to Node* instead of Node** and dereferences incorrectly, causing wrong sorting.
🚀 Application
advanced2:30remaining
Huffman Encoding Compression Ratio Calculation
A file contains 1000 characters: 400 'A's, 300 'B's, 200 'C's, and 100 'D's. Using Huffman encoding, what is the approximate compression ratio compared to fixed-length 2-bit encoding per character?
Attempts:
2 left
💡 Hint
Calculate average bits per character using Huffman codes and compare with fixed 2 bits per character.
✗ Incorrect
Fixed-length encoding uses 2 bits per character. Huffman encoding assigns shorter codes to frequent characters, reducing average bits per character. Calculating weighted average bits per character gives about 1.2 bits, so compression ratio = 1.2/2 = 0.6.
🧠 Conceptual
expert2:00remaining
Huffman Encoding Tree Uniqueness
Which statement about the uniqueness of Huffman encoding trees is correct when multiple characters have the same frequency?
Attempts:
2 left
💡 Hint
Consider how the algorithm chooses nodes when frequencies are equal.
✗ Incorrect
When frequencies tie, the order of combining nodes can vary, leading to different valid Huffman trees with different codes but same total cost.