Huffman Encoding is a way to compress data by using shorter codes for common letters and longer codes for rare letters. It builds a tree to assign these codes so the total size is smaller.
Click to reveal answer
beginner
What data structure is mainly used in Huffman Encoding?
A binary tree called the Huffman Tree is used. It helps assign codes to characters based on their frequency.
Click to reveal answer
intermediate
How are the codes assigned in Huffman Encoding?
Codes are assigned by traversing the Huffman Tree: going left adds '0' and going right adds '1'. Leaves represent characters with their codes.
Click to reveal answer
intermediate
Why does Huffman Encoding produce a prefix code?
Because no code is the start of another code, so decoding is easy and unambiguous. This happens naturally from the tree structure.
Click to reveal answer
advanced
What is the time complexity to build a Huffman Tree for n unique characters?
It is O(n log n) because we use a priority queue to pick the two smallest frequencies repeatedly until one tree remains.
Click to reveal answer
What does Huffman Encoding use to decide code lengths?
ACharacter frequency
BAlphabetical order
CCharacter ASCII value
DRandom assignment
✗ Incorrect
Huffman Encoding assigns shorter codes to characters that appear more often, based on their frequency.
In Huffman Tree, what does a leaf node represent?
AA character and its code
BA frequency sum
CAn internal node
DA code prefix
✗ Incorrect
Leaf nodes represent actual characters with their assigned codes.
What is the first step in building a Huffman Tree?
ABuild a balanced binary tree
BCreate leaf nodes for each character with frequency
CAssign codes randomly
DSort characters alphabetically
✗ Incorrect
We start by creating leaf nodes for each character with their frequency.
Why is Huffman Encoding called a lossless compression?
ABecause it changes data format
BBecause it removes some data
CBecause it uses lossy codes
DBecause original data can be perfectly restored
✗ Incorrect
Huffman Encoding allows exact recovery of original data, so no information is lost.
Which data structure helps efficiently pick the two smallest frequencies in Huffman Encoding?
AStack
BQueue
CPriority queue (min-heap)
DLinked list
✗ Incorrect
A priority queue or min-heap helps pick the two smallest frequency nodes quickly.
Explain how Huffman Encoding builds the tree and assigns codes to characters.
Think about combining smallest frequencies step by step.
You got /5 concepts.
Why is Huffman Encoding efficient for data compression compared to fixed-length codes?
Consider how code length relates to character frequency.
You got /5 concepts.
Practice
(1/5)
1. What is the main purpose of Huffman Encoding in data compression?
easy
A. To sort characters alphabetically
B. To assign equal length codes to all characters
C. To encrypt data for security
D. To assign shorter codes to more frequent characters
Solution
Step 1: Understand Huffman Encoding goal
Huffman Encoding aims to reduce the total bits used by giving shorter codes to frequent characters.
Step 2: Compare options with this goal
Only To assign shorter codes to more frequent characters matches this goal. Others describe different or incorrect purposes.
Final Answer:
To assign shorter codes to more frequent characters -> Option D
Quick Check:
Huffman Encoding = shorter codes for frequent chars [OK]
Hint: Remember: frequent chars get shorter codes in Huffman [OK]
Common Mistakes:
Thinking Huffman assigns equal length codes
Confusing Huffman with encryption
Assuming it sorts characters alphabetically
2. Which TypeScript data structure is best suited to build the priority queue for Huffman Encoding?
easy
A. Array with manual sorting
B. Min-Heap
C. Set
D. Map
Solution
Step 1: Identify priority queue needs
Huffman Encoding requires repeatedly extracting the smallest frequency node efficiently.
Step 2: Choose data structure for efficient min extraction
Min-Heap allows quick access and removal of the smallest element, ideal for this use.
Final Answer:
Min-Heap -> Option B
Quick Check:
Priority queue = Min-Heap for Huffman [OK]
Hint: Use Min-Heap for efficient smallest element extraction [OK]
Common Mistakes:
Using Set which has no order
Using Map which is key-value, not priority queue
Using Array without sorting is inefficient
3. What is the output Huffman code for characters {a:5, b:9, c:12, d:13, e:16, f:45} after building the Huffman tree?
Combine lowest frequencies stepwise: (a=5,b=9)->14, (c=12,d=13)->25, then combine 14 and 16 (e) and so on, finally f=45 is highest frequency with shortest code.
Step 2: Assign codes from tree paths
Left edges as '0', right edges as '1' produce codes: f='0', c='100', d='101', a='1100', b='1101', e='111'.
Hint: Highest frequency gets shortest code '0' in Huffman [OK]
Common Mistakes:
Assigning codes without building tree
Mixing left/right edge bits
Ignoring frequency order
4. Given this TypeScript snippet for Huffman tree node insertion, what is the bug?
class Node {
constructor(public char: string | null, public freq: number, public left: Node | null = null, public right: Node | null = null) {}
}
function insertNode(queue: Node[], node: Node) {
queue.push(node);
queue.sort((a, b) => a.freq - b.freq);
}
// Usage
const queue: Node[] = [];
insertNode(queue, new Node('a', 5));
insertNode(queue, new Node('b', 9));
insertNode(queue, new Node(null, 14));
medium
A. Sorting after push is inefficient; use a Min-Heap instead
B. Node constructor missing type annotations
C. Function insertNode should remove duplicates
D. queue.sort comparator should be reversed
Solution
Step 1: Analyze insertNode function
It pushes a node then sorts the entire queue array by frequency each time.
Step 2: Identify inefficiency
Sorting the whole array on every insertion is inefficient for large data; a Min-Heap is better for priority queue operations.
Final Answer:
Sorting after push is inefficient; use a Min-Heap instead -> Option A
Quick Check:
Priority queue needs efficient insert and extract [OK]
Hint: Use Min-Heap, not array sort, for priority queue [OK]
Common Mistakes:
Thinking sorting order is wrong
Missing inefficiency of sorting whole array
Confusing type annotations as bug
5. You have a string "aaabbc" and want to encode it using Huffman Encoding. Which of the following is the correct Huffman code assignment and encoded bit string?
hard
A. Codes: {a:'0', b:'10', c:'11'}, Encoded: '000101011'
B. Codes: {a:'10', b:'0', c:'11'}, Encoded: '101010011'
C. Codes: {a:'0', b:'11', c:'10'}, Encoded: '00011110'
D. Codes: {a:'1', b:'0', c:'10'}, Encoded: '1110010'
Solution
Step 1: Calculate frequencies
String "aaabbc" has frequencies: a=3, b=2, c=1.
Step 2: Build Huffman tree and assign codes
Lowest freq c=1 and b=2 combine first, then with a=3. Assign codes: a='0', b='10', c='11'.