0
0
DSA Typescriptprogramming~20 mins

Huffman Encoding in DSA Typescript - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Huffman Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Huffman tree frequency merge step?
Given the initial frequency list [5, 9, 12, 13, 16, 45], after the first merge step in Huffman encoding, what is the new list of frequencies?
DSA Typescript
const frequencies = [5, 9, 12, 13, 16, 45];
// First merge step merges two smallest frequencies
// What is the new frequency list after this step?
A[5, 9, 12, 13, 61]
B[5, 9, 25, 16, 45]
C[14, 12, 13, 16, 45]
D[9, 12, 13, 16, 45]
Attempts:
2 left
💡 Hint
Merge the two smallest frequencies by adding them and replace them with their sum.
🧠 Conceptual
intermediate
1:30remaining
What property does Huffman encoding guarantee about the codes?
Which property is always true for the codes generated by Huffman encoding?
ANo code is a prefix of another code.
BCodes are assigned randomly to characters.
CAll codes have the same length.
DCodes are always 8 bits long.
Attempts:
2 left
💡 Hint
Think about how Huffman codes avoid ambiguity when decoding.
🔧 Debug
advanced
2:00remaining
What error occurs in this TypeScript Huffman tree node creation?
Consider this TypeScript snippet for creating a Huffman tree node: ```typescript class Node { freq: number; left?: Node; right?: Node; constructor(freq: number, left?: Node, right?: Node) { this.freq = freq; this.left = left; this.right = right; } } const node = new Node(); ``` What error will this code produce?
DSA Typescript
class Node {
  freq: number;
  left?: Node;
  right?: Node;
  constructor(freq: number, left?: Node, right?: Node) {
    this.freq = freq;
    this.left = left;
    this.right = right;
  }
}

const node = new Node();
AError: Expected 1-3 arguments, but got 0.
BError: Property 'freq' is missing in type '{}'.
CNo error, node created with freq=0.
DError: Cannot assign undefined to 'freq'.
Attempts:
2 left
💡 Hint
Check the constructor parameters and how you call it.
🚀 Application
advanced
2:30remaining
How many bits are saved by Huffman encoding this string?
Given the string 'aaabbc', frequencies are: a=3, b=2, c=1. Using Huffman encoding, how many bits are saved compared to fixed 2-bit encoding per character?
A4 bits saved
B3 bits saved
C5 bits saved
D6 bits saved
Attempts:
2 left
💡 Hint
Calculate total bits with fixed length and with Huffman codes, then find the difference.
Predict Output
expert
3:00remaining
What is the Huffman code for character 'd' after building this tree?
Given the frequency map {a: 5, b: 9, c: 12, d: 13, e: 16, f: 45}, after building the Huffman tree, what is the code for 'd'?
DSA Typescript
/*
Frequencies: a=5, b=9, c=12, d=13, e=16, f=45
Build Huffman tree and find code for 'd'.
*/
A'010'
B'011'
C'100'
D'101'
Attempts:
2 left
💡 Hint
Build the tree step-by-step and track the path to 'd' (left=0, right=1).