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 Go code that builds a Huffman tree and prints the root node frequency?
DSA Go
package main import ( "container/heap" "fmt" ) type Node struct { char rune freq int left *Node right *Node } type PriorityQueue []*Node func (pq PriorityQueue) Len() int { return len(pq) } func (pq PriorityQueue) Less(i, j int) bool { return pq[i].freq < pq[j].freq } func (pq PriorityQueue) Swap(i, j int) { pq[i], pq[j] = pq[j], pq[i] } func (pq *PriorityQueue) Push(x interface{}) { *pq = append(*pq, x.(*Node)) } func (pq *PriorityQueue) Pop() interface{} { n := len(*pq) item := (*pq)[n-1] *pq = (*pq)[:n-1] return item } func buildHuffmanTree(freqMap map[rune]int) *Node { pq := &PriorityQueue{} heap.Init(pq) for ch, freq := range freqMap { heap.Push(pq, &Node{char: ch, freq: freq}) } for pq.Len() > 1 { left := heap.Pop(pq).(*Node) right := heap.Pop(pq).(*Node) merged := &Node{freq: left.freq + right.freq, left: left, right: right} heap.Push(pq, merged) } return heap.Pop(pq).(*Node) } func main() { freqMap := map[rune]int{'a': 5, 'b': 9, 'c': 12, 'd': 13, 'e': 16, 'f': 45} root := buildHuffmanTree(freqMap) fmt.Println(root.freq) }
Attempts:
2 left
💡 Hint
Sum all frequencies to find the root node frequency.
✗ Incorrect
The root node frequency is the sum of all character frequencies: 5+9+12+13+16+45 = 100.
🧠 Conceptual
intermediateHuffman Encoding Bit Length Calculation
Given the frequencies {'a': 3, 'b': 2, 'c': 6, 'd': 8}, what is the total number of bits required to encode the string "abcd" repeated 5 times using Huffman encoding?
Attempts:
2 left
💡 Hint
Calculate the Huffman codes for each character, then multiply by their frequencies in the repeated string.
✗ Incorrect
The Huffman codes assign shorter codes to higher frequency chars. After building the tree, the total bits for 5 repetitions of "abcd" (20 chars) is 45 bits.
🔧 Debug
advancedIdentify the Error in Huffman Encoding Implementation
What error will the following Go code produce when trying to build a Huffman tree?
DSA Go
package main import ( "container/heap" "fmt" ) type Node struct { char rune freq int left *Node right *Node } type PriorityQueue []*Node func (pq PriorityQueue) Len() int { return len(pq) } func (pq PriorityQueue) Less(i, j int) bool { return pq[i].freq > pq[j].freq } func (pq PriorityQueue) Swap(i, j int) { pq[i], pq[j] = pq[j], pq[i] } func (pq *PriorityQueue) Push(x interface{}) { *pq = append(*pq, x.(*Node)) } func (pq *PriorityQueue) Pop() interface{} { n := len(*pq) item := (*pq)[n-1] *pq = (*pq)[:n-1] return item } func buildHuffmanTree(freqMap map[rune]int) *Node { pq := &PriorityQueue{} heap.Init(pq) for ch, freq := range freqMap { heap.Push(pq, &Node{char: ch, freq: freq}) } for pq.Len() > 1 { left := heap.Pop(pq).(*Node) right := heap.Pop(pq).(*Node) merged := &Node{freq: left.freq + right.freq, left: left, right: right} heap.Push(pq, merged) } return heap.Pop(pq).(*Node) } func main() { freqMap := map[rune]int{'a': 5, 'b': 9, 'c': 12} root := buildHuffmanTree(freqMap) fmt.Println(root.freq) }
Attempts:
2 left
💡 Hint
Check the Less function in the priority queue implementation.
✗ Incorrect
The Less function uses '>' which makes the heap a max-heap, but Huffman requires a min-heap to combine lowest frequencies first. This causes incorrect tree construction.
📝 Syntax
advancedIdentify the Syntax Error in Huffman Encoding Code
Which option contains the syntax error in this Go code snippet for Huffman encoding?
DSA Go
type Node struct { char rune freq int left *Node right *Node } func buildHuffmanTree(freqMap map[rune]int) *Node { pq := &PriorityQueue{} heap.Init(pq) for ch, freq := range freqMap { heap.Push(pq, &Node{char: ch, freq: freq}) } for pq.Len() > 1 { left := heap.Pop(pq).(*Node) right := heap.Pop(pq).(*Node) merged := &Node{freq: left.freq + right.freq, left: left, right: right} heap.Push(pq, merged) } return heap.Pop(pq).(*Node) }
Attempts:
2 left
💡 Hint
Check the imports needed for heap operations.
✗ Incorrect
The code uses heap.Init and heap.Push but does not import "container/heap", causing a syntax error.
🚀 Application
expertCalculate Huffman Encoding Output for Given Frequencies
Given the frequencies {'x': 10, 'y': 15, 'z': 30, 'w': 45}, what is the Huffman code length for character 'y' after building the Huffman tree?
Attempts:
2 left
💡 Hint
Build the Huffman tree step-by-step and count the depth of 'y'.
✗ Incorrect
The Huffman tree merges lowest frequencies first. 'y' with frequency 15 ends up at depth 3, so its code length is 3 bits.
