Challenge - 5 Problems
Huffman Mastery
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 root node frequency?
DSA C#
using System; using System.Collections.Generic; class Node { public char? Symbol; public int Frequency; public Node Left, Right; public Node(char? symbol, int freq) { Symbol = symbol; Frequency = freq; Left = Right = null; } } class Program { static void Main() { var frequencies = new Dictionary<char, int> {{'a', 5}, {'b', 9}, {'c', 12}, {'d', 13}, {'e', 16}, {'f', 45}}; var pq = new SortedSet<(int freq, int id, Node node)>(Comparer<(int, int, Node)>.Create((x, y) => { int cmp = x.freq.CompareTo(y.freq); if (cmp == 0) return x.id.CompareTo(y.id); return cmp; })); int id = 0; foreach (var kvp in frequencies) { pq.Add((kvp.Value, id++, new Node(kvp.Key, kvp.Value))); } while (pq.Count > 1) { var first = pq.Min; pq.Remove(first); var second = pq.Min; pq.Remove(second); var merged = new Node(null, first.freq + second.freq); merged.Left = first.node; merged.Right = second.node; pq.Add((merged.Frequency, id++, merged)); } var root = pq.Min.node; Console.WriteLine(root.Frequency); } }
Attempts:
2 left
💡 Hint
Sum all frequencies to find the root frequency.
✗ Incorrect
The root node frequency is the sum of all individual character frequencies: 5 + 9 + 12 + 13 + 16 + 45 = 100.
🧠 Conceptual
intermediateHuffman Encoding Code Length Calculation
Given a Huffman tree built from characters with frequencies {'a': 3, 'b': 2, 'c': 6, 'd': 8}, what is the total number of bits needed to encode the string "abcd" using the Huffman codes?
Attempts:
2 left
💡 Hint
Calculate the code length for each character based on the Huffman tree and multiply by frequency in the string.
✗ Incorrect
The Huffman codes assign shorter codes to more frequent characters. After building the tree, the codes lengths are: 'a':3 bits, 'b':3 bits, 'c':2 bits, 'd':1 bit. Encoding "abcd" uses 3 + 3 + 2 + 1 = 9 bits, but since the string is "abcd" (one of each), total bits = 3+3+2+1=9 bits. The correct answer is 9 bits.
🔧 Debug
advancedIdentify the Error in Huffman Tree Node Comparison
What error will occur when running this C# code snippet that uses a SortedSet to build a Huffman tree?
DSA C#
var pq = new SortedSet<Node>(); // Node class does not implement IComparable pq.Add(new Node('a', 5)); pq.Add(new Node('b', 9)); var first = pq.Min;
Attempts:
2 left
💡 Hint
SortedSet requires elements to be comparable or a comparer to be provided.
✗ Incorrect
SortedSet needs to compare elements to maintain order. Without IComparable or a comparer, it throws InvalidOperationException.
🚀 Application
advancedHuffman Encoding Compression Ratio Calculation
A text contains 1000 characters with frequencies: {'x': 500, 'y': 300, 'z': 200}. Using Huffman encoding, the average bits per character is 1.5 bits. What is the compression ratio compared to using 8 bits per character fixed-length encoding?
Attempts:
2 left
💡 Hint
Compression ratio = (compressed size / original size) * 100%
✗ Incorrect
Original size = 1000 * 8 = 8000 bits. Compressed size = 1000 * 1.5 = 1500 bits. Ratio = (1500 / 8000) * 100% = 18.75%. The question asks for compression ratio compared to fixed-length encoding, which is the percentage of original size used, so 18.75%.
❓ Predict Output
expertOutput of Huffman Encoding Map Generation
What is the output of the following C# code that generates Huffman codes for characters and prints the code for 'b'?
DSA C#
using System; using System.Collections.Generic; class Node { public char? Symbol; public int Frequency; public Node Left, Right; public Node(char? symbol, int freq) { Symbol = symbol; Frequency = freq; Left = Right = null; } } class Program { static void BuildCodes(Node node, string code, Dictionary<char, string> codes) { if (node == null) return; if (node.Symbol != null) { codes[node.Symbol.Value] = code; return; } BuildCodes(node.Left, code + "0", codes); BuildCodes(node.Right, code + "1", codes); } static void Main() { var frequencies = new Dictionary<char, int> {{'a', 1}, {'b', 1}, {'c', 2}, {'d', 4}}; var pq = new SortedSet<(int freq, int id, Node node)>(Comparer<(int, int, Node)>.Create((x, y) => { int cmp = x.freq.CompareTo(y.freq); if (cmp == 0) return x.id.CompareTo(y.id); return cmp; })); int id = 0; foreach (var kvp in frequencies) { pq.Add((kvp.Value, id++, new Node(kvp.Key, kvp.Value))); } while (pq.Count > 1) { var first = pq.Min; pq.Remove(first); var second = pq.Min; pq.Remove(second); var merged = new Node(null, first.freq + second.freq); merged.Left = first.node; merged.Right = second.node; pq.Add((merged.Frequency, id++, merged)); } var root = pq.Min.node; var codes = new Dictionary<char, string>(); BuildCodes(root, "", codes); Console.WriteLine(codes['b']); } }
Attempts:
2 left
💡 Hint
Trace the tree construction and code assignment carefully.
✗ Incorrect
The Huffman tree merges 'a'(1) and 'b'(1) first, assigning 'a' code '00' and 'b' code '01'. Thus, 'b' code is '01'.
