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
Given the following frequencies for characters, what is the total weight of the root node after building the Huffman tree?
Characters and frequencies: A=5, B=9, C=12, D=13, E=16, F=45
Characters and frequencies: A=5, B=9, C=12, D=13, E=16, F=45
DSA Java
import java.util.*; class Node implements Comparable<Node> { int freq; char ch; Node left, right; Node(char ch, int freq) { this.ch = ch; this.freq = freq; } Node(int freq, Node left, Node right) { this.freq = freq; this.left = left; this.right = right; } public int compareTo(Node other) { return this.freq - other.freq; } } public class HuffmanTree { public static Node buildTree(Map<Character, Integer> freqMap) { PriorityQueue<Node> pq = new PriorityQueue<>(); for (Map.Entry<Character, Integer> entry : freqMap.entrySet()) { pq.add(new Node(entry.getKey(), entry.getValue())); } while (pq.size() > 1) { Node left = pq.poll(); Node right = pq.poll(); Node parent = new Node(left.freq + right.freq, left, right); pq.add(parent); } return pq.poll(); } public static void main(String[] args) { Map<Character, Integer> freqMap = new HashMap<>(); freqMap.put('A', 5); freqMap.put('B', 9); freqMap.put('C', 12); freqMap.put('D', 13); freqMap.put('E', 16); freqMap.put('F', 45); Node root = buildTree(freqMap); System.out.println(root.freq); } }
Attempts:
2 left
💡 Hint
Sum all frequencies and check how Huffman tree merges nodes.
✗ Incorrect
The total frequency sum is 5+9+12+13+16+45 = 100. The root node frequency equals the sum of all frequencies.
🧠 Conceptual
intermediateUnderstanding Huffman Code Lengths
In Huffman encoding, which character will always have the longest code?
Attempts:
2 left
💡 Hint
Think about how Huffman tree places characters with different frequencies.
✗ Incorrect
Characters with lower frequency are placed deeper in the Huffman tree, resulting in longer codes.
🔧 Debug
advancedIdentify the Error in Huffman Encoding Code
What error will this code produce when building a Huffman tree?
DSA Java
import java.util.*; class Node implements Comparable<Node> { int freq; char ch; Node left, right; Node(char ch, int freq) { this.ch = ch; this.freq = freq; } Node(int freq, Node left, Node right) { this.freq = freq; this.left = left; this.right = right; } public int compareTo(Node other) { return other.freq - this.freq; // Descending order } } public class HuffmanTree { public static Node buildTree(Map<Character, Integer> freqMap) { PriorityQueue<Node> pq = new PriorityQueue<>(); for (Map.Entry<Character, Integer> entry : freqMap.entrySet()) { pq.add(new Node(entry.getKey(), entry.getValue())); } while (pq.size() > 1) { Node left = pq.poll(); Node right = pq.poll(); Node parent = new Node(left.freq + right.freq, left, right); pq.add(parent); } return pq.poll(); } }
Attempts:
2 left
💡 Hint
Check the compareTo method and how PriorityQueue orders elements.
✗ Incorrect
The compareTo method returns descending order, but Huffman requires ascending order to build the tree correctly. This causes incorrect tree structure.
🚀 Application
advancedHuffman Encoding Output Codes
Given the frequencies: A=3, B=2, C=6, D=8, what is the Huffman code for character 'B' after building the tree?
Attempts:
2 left
💡 Hint
Build the Huffman tree step-by-step and assign 0 to left and 1 to right edges.
✗ Incorrect
After building the tree, 'B' is assigned the code '110' based on its position in the tree.
🧠 Conceptual
expertHuffman Encoding Optimality Proof Concept
Which statement best explains why Huffman encoding produces an optimal prefix code?
Attempts:
2 left
💡 Hint
Think about how Huffman tree minimizes the total bits used.
✗ Incorrect
Huffman encoding builds a binary tree that minimizes the sum of frequency times code length, producing the shortest average code length.
