Challenge - 5 Problems
Huffman Encoding Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Huffman Tree Construction
What is the output of the following C++ code that builds a Huffman tree and prints the codes for each character?
DSA C++
#include <iostream> #include <queue> #include <unordered_map> #include <vector> using namespace std; struct Node { char ch; int freq; Node *left, *right; Node(char c, int f) : ch(c), freq(f), left(nullptr), right(nullptr) {} }; struct Compare { bool operator()(Node* l, Node* r) { return l->freq > r->freq; } }; void printCodes(Node* root, string str) { if (!root) return; if (!root->left && !root->right) { cout << root->ch << ":" << str << " "; } printCodes(root->left, str + "0"); printCodes(root->right, str + "1"); } int main() { unordered_map<char, int> freq = {{'a', 5}, {'b', 9}, {'c', 12}, {'d', 13}, {'e', 16}, {'f', 45}}; priority_queue<Node*, vector<Node*>, Compare> pq; for (auto pair : freq) { pq.push(new Node(pair.first, pair.second)); } while (pq.size() > 1) { Node* left = pq.top(); pq.pop(); Node* right = pq.top(); pq.pop(); Node* merged = new Node('$', left->freq + right->freq); merged->left = left; merged->right = right; pq.push(merged); } Node* root = pq.top(); printCodes(root, ""); return 0; }
Attempts:
2 left
💡 Hint
Focus on how the priority queue merges nodes with lowest frequencies first.
✗ Incorrect
The Huffman tree merges nodes with the smallest frequencies first, assigning shorter codes to more frequent characters. 'f' has the highest frequency and gets the shortest code '0'. The codes for other characters follow from the tree structure.
🧠 Conceptual
intermediateHuffman Encoding Tree Property
Which property is always true for a Huffman encoding tree built from a set of character frequencies?
Attempts:
2 left
💡 Hint
Think about how nodes are merged in pairs during construction.
✗ Incorrect
Huffman trees are full binary trees because each internal node is created by merging exactly two nodes, so every internal node has two children.
🔧 Debug
advancedIdentify the Error in Huffman Encoding Code
What error will the following C++ code produce when run?
DSA C++
#include <iostream> #include <queue> #include <unordered_map> using namespace std; struct Node { char ch; int freq; Node *left, *right; Node(char c, int f) : ch(c), freq(f), left(nullptr), right(nullptr) {} }; struct Compare { bool operator()(Node* l, Node* r) { return l->freq < r->freq; // Note the comparison direction } }; int main() { unordered_map<char, int> freq = {{'a', 5}, {'b', 9}}; priority_queue<Node*, vector<Node*>, Compare> pq; for (auto pair : freq) { pq.push(new Node(pair.first, pair.second)); } while (pq.size() > 1) { Node* left = pq.top(); pq.pop(); Node* right = pq.top(); pq.pop(); Node* merged = new Node('$', left->freq + right->freq); merged->left = left; merged->right = right; pq.push(merged); } cout << pq.top()->freq << endl; return 0; }
Attempts:
2 left
💡 Hint
Check the priority queue comparison operator and how it affects the order.
✗ Incorrect
The comparison operator uses '<', making the priority_queue a max-heap (pops largest frequencies first). With only two nodes, it still correctly merges them and prints the total frequency 14.
🚀 Application
advancedCalculate Total Bits for Huffman Encoding
Given characters with frequencies {'x': 10, 'y': 15, 'z': 30, 'w': 45}, what is the total number of bits required to encode the string using Huffman encoding?
Attempts:
2 left
💡 Hint
Build the Huffman tree, assign codes, then multiply code length by frequency and sum.
✗ Incorrect
The Huffman codes are: w=0 (1 bit), z=11 (2 bits), y=101 (3 bits), x=100 (3 bits). Total bits = 45*1 + 30*2 + 15*3 + 10*3 = 45 + 60 + 45 + 30 = 180 bits.
❓ Predict Output
expertOutput of Huffman Decoding Function
What is the output of the following C++ code that decodes a Huffman encoded string?
DSA C++
#include <iostream> #include <unordered_map> #include <string> using namespace std; string decodeHuffman(string encoded, unordered_map<string, char> codeMap) { string result = ""; string temp = ""; for (char bit : encoded) { temp += bit; if (codeMap.find(temp) != codeMap.end()) { result += codeMap[temp]; temp = ""; } } return result; } int main() { unordered_map<string, char> codeMap = {{"0", 'a'}, {"10", 'b'}, {"110", 'c'}, {"111", 'd'}}; string encoded = "010110111010"; cout << decodeHuffman(encoded, codeMap) << endl; return 0; }
Attempts:
2 left
💡 Hint
Match bits from left to right using the code map to decode characters.
✗ Incorrect
Decoding steps:
'0' -> 'a'
'10' -> 'b'
'110' -> 'c'
'111' -> 'd'
'0' -> 'a'
'10' -> 'b'
Decoded string: abcdab. Matches option C.
