0
0
DSA Cprogramming~20 mins

Huffman Encoding in DSA C - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Huffman Encoding Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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: 111
A{ "f": "111", "c": "110", "d": "101", "a": "100", "b": "0", "e": "10" }
B{ "a": "0", "b": "10", "c": "110", "d": "1110", "e": "11110", "f": "11111" }
C{ "a": "00", "b": "01", "c": "10", "d": "110", "e": "111", "f": "1" }
D{ "f": "0", "c": "100", "d": "101", "a": "1100", "b": "1101", "e": "111" }
Attempts:
2 left
💡 Hint
Recall that characters with higher frequency get shorter codes in Huffman encoding.
🧠 Conceptual
intermediate
1: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?
A12 bits
B16 bits
C10 bits
D14 bits
Attempts:
2 left
💡 Hint
Calculate the Huffman codes for each character and multiply by their occurrences in the string.
🔧 Debug
advanced
2: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
AIncorrect sorting because pointers are not dereferenced properly
BSegmentation fault due to incorrect pointer casting
CCompilation error due to incompatible pointer types
DNo error; code works correctly
Attempts:
2 left
💡 Hint
Consider what qsort passes to the compare function when sorting pointers.
🚀 Application
advanced
2: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?
ACompression ratio is approximately 1.25 (25% smaller)
BCompression ratio is approximately 0.6 (40% smaller)
CCompression ratio is approximately 0.8 (20% smaller)
DCompression ratio is approximately 0.5 (50% smaller)
Attempts:
2 left
💡 Hint
Calculate average bits per character using Huffman codes and compare with fixed 2 bits per character.
🧠 Conceptual
expert
2:00remaining
Huffman Encoding Tree Uniqueness
Which statement about the uniqueness of Huffman encoding trees is correct when multiple characters have the same frequency?
AMultiple valid Huffman trees can exist if characters have equal frequencies
BHuffman encoding tree is always unique regardless of frequency ties
CHuffman tree uniqueness depends on the order characters appear in the input string
DHuffman tree is unique only if all frequencies are distinct prime numbers
Attempts:
2 left
💡 Hint
Consider how the algorithm chooses nodes when frequencies are equal.