Bird
Raised Fist0
DSA Rustprogramming~10 mins

Huffman Encoding in DSA Rust - Interactive Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a frequency map from a string.

DSA Rust
let mut freq_map = std::collections::HashMap::new();
for ch in text.chars() {
    *freq_map.entry(ch).or_insert([1]) += 1;
}
Drag options to blanks, or click blank then click option'
A1
Btrue
CNone
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using 1 instead of 0 causes counts to start at 2.
Using None causes a type error.
2fill in blank
medium

Complete the code to pop the smallest frequency node from a binary heap.

DSA Rust
let node = heap.pop().[1]();
Drag options to blanks, or click blank then click option'
Aunwrap_or
Bunwrap
Cexpect
Dunwrap_err
Attempts:
3 left
💡 Hint
Common Mistakes
Using unwrap_err() causes panic because pop() returns Some or None, not an error.
Using expect() requires a message string.
3fill in blank
hard

Fix the error in the code to insert a new node into the heap.

DSA Rust
heap.[1](new_node);
Drag options to blanks, or click blank then click option'
Apush
Binsert
Cadd
Dappend
Attempts:
3 left
💡 Hint
Common Mistakes
Using insert() causes a compile error.
Using append() is for vectors, not heaps.
4fill in blank
hard

Fill both blanks to build a recursive function that assigns codes to characters.

DSA Rust
fn assign_codes(node: &Node, prefix: String, codes: &mut std::collections::HashMap<char, String>) {
    if node.left.is_none() && node.right.is_none() {
        codes.insert(node.ch.unwrap(), prefix);
    } else {
        if let Some(ref left) = node.left {
            assign_codes(left, prefix.clone() + [1], codes);
        }
        if let Some(ref right) = node.right {
            assign_codes(right, prefix.clone() + [2], codes);
        }
    }
}
Drag options to blanks, or click blank then click option'
A"0"
B"1"
C""
D" "
Attempts:
3 left
💡 Hint
Common Mistakes
Using empty strings causes all codes to be the same.
Using spaces causes incorrect codes.
5fill in blank
hard

Fill all three blanks to create a frequency map, build a heap, and pop the smallest node.

DSA Rust
let mut freq_map = std::collections::HashMap::new();
for ch in text.chars() {
    *freq_map.entry(ch).or_insert([1]) += 1;
}

let mut heap = std::collections::BinaryHeap::new();
for (ch, freq) in freq_map {
    heap.[2](Node::new_leaf(ch, freq));
}

let smallest = heap.pop().[3]();
Drag options to blanks, or click blank then click option'
A0
Bpush
Cunwrap
D1
Attempts:
3 left
💡 Hint
Common Mistakes
Starting counts at 1 causes off-by-one errors.
Using insert instead of push causes errors.
Not unwrapping pop() causes type errors.

Practice

(1/5)
1. What is the main purpose of Huffman Encoding in data compression?
easy
A. To assign shorter codes to more frequent characters
B. To assign equal length codes to all characters
C. To encrypt data for security
D. To sort characters alphabetically

Solution

  1. Step 1: Understand Huffman Encoding goal

    Huffman Encoding compresses data by giving shorter codes to characters that appear more often.
  2. Step 2: Compare options with this goal

    Only To assign shorter codes to more frequent characters matches this goal; others describe different concepts.
  3. Final Answer:

    To assign shorter codes to more frequent characters -> Option A
  4. 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 compression with encryption
  • Assuming sorting is the main goal
2. Which Rust data structure is best suited to implement the priority queue needed for Huffman Encoding?
easy
A. BinaryHeap<T>
B. HashMap
C. Vec<T>
D. LinkedList<T>

Solution

  1. Step 1: Identify priority queue requirement

    Huffman Encoding needs a structure to get the smallest frequency element quickly.
  2. Step 2: Match Rust structures to priority queue

    BinaryHeap<T> is Rust's built-in priority queue supporting efficient min/max operations.
  3. Final Answer:

    BinaryHeap<T> -> Option A
  4. Quick Check:

    Priority queue = BinaryHeap in Rust [OK]
Hint: Use BinaryHeap for priority queues in Rust [OK]
Common Mistakes:
  • Using Vec without sorting for priority queue
  • Choosing HashMap which is for key-value pairs
  • Using LinkedList which is inefficient here
3. What is the output of the following Rust code snippet that builds a Huffman tree for characters with frequencies: {'a': 5, 'b': 9, 'c': 12}?
use std::collections::BinaryHeap;

#[derive(Eq, PartialEq)]
struct Node {
    freq: usize,
    char_opt: Option<char>,
}

impl Ord for Node {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        other.freq.cmp(&self.freq) // min-heap behavior
    }
}

impl PartialOrd for Node {
    fn partial_cmp(&self, other: &Self) -> Option {
        Some(self.cmp(other))
    }
}

fn main() {
    let mut heap = BinaryHeap::new();
    heap.push(Node { freq: 5, char_opt: Some('a') });
    heap.push(Node { freq: 9, char_opt: Some('b') });
    heap.push(Node { freq: 12, char_opt: Some('c') });

    let first = heap.pop().unwrap();
    let second = heap.pop().unwrap();

    println!("{} {}", first.freq, second.freq);
}
medium
A. 9 5
B. 12 9
C. 5 9
D. 5 12

Solution

  1. Step 1: Understand BinaryHeap with custom Ord

    The cmp reverses order to make a min-heap by frequency, so pop() returns smallest freq first.
  2. Step 2: Pop two smallest frequencies

    Frequencies are 5, 9, 12. First pop returns 5, second pop returns 9.
  3. Final Answer:

    5 9 -> Option C
  4. Quick Check:

    Min-heap pops smallest first = 5 9 [OK]
Hint: Custom Ord reverses cmp for min-heap pop order [OK]
Common Mistakes:
  • Assuming BinaryHeap is max-heap by default
  • Mixing order of popped elements
  • Ignoring custom Ord implementation
4. Identify the bug in this Rust code snippet that tries to build a Huffman tree node by combining two nodes:
struct Node {
    freq: usize,
    left: Option,
    right: Option,
}

fn combine_nodes(left: Node, right: Node) -> Node {
    Node {
        freq: left.freq + right.freq,
        left: Some(Box::new(left)),
        right: Some(Box::new(right)),
    }
}

fn main() {
    let n1 = Node { freq: 3, left: None, right: None };
    let n2 = Node { freq: 4, left: None, right: None };
    let parent = combine_nodes(n1, n2);
    println!("Parent freq: {}", parent.freq);
}
medium
A. Cannot use Box::new with struct Node
B. Missing lifetime annotations for Box<Node>
C. Function should take references, not owned Nodes
D. No bug, code compiles and runs correctly

Solution

  1. Step 1: Check ownership and Box usage

    Function takes owned Nodes and boxes them correctly for left and right fields.
  2. Step 2: Verify code compiles and runs

    Code compiles and prints combined frequency without errors.
  3. Final Answer:

    No bug, code compiles and runs correctly -> Option D
  4. Quick Check:

    Ownership and Box usage correct = no bug [OK]
Hint: Owned nodes boxed correctly means no bug here [OK]
Common Mistakes:
  • Assuming lifetime needed for Box<Node> here
  • Thinking references required instead of ownership
  • Believing Box::new cannot wrap structs
5. Given character frequencies {'x': 7, 'y': 3, 'z': 2, 'w': 6}, which of the following Huffman codes is valid and optimal?
hard
A. x: 11, y: 10, z: 0, w: 1
B. x: 0, y: 10, z: 110, w: 111
C. x: 10, y: 0, z: 111, w: 110
D. x: 0, y: 1, z: 10, w: 11

Solution

  1. Step 1: Order characters by frequency

    Frequencies: z=2, y=3, w=6, x=7. Huffman assigns shortest codes to highest freq.
  2. Step 2: Check code lengths and prefix property

    x: 0, y: 10, z: 110, w: 111 assigns shortest code '0' to x (freq 7), longer codes to lower freq chars, and codes are prefix-free.
  3. Final Answer:

    x: 0, y: 10, z: 110, w: 111 -> Option B
  4. Quick Check:

    Shortest code to highest freq = x: 0, y: 10, z: 110, w: 111 [OK]
Hint: Shortest code goes to highest frequency char [OK]
Common Mistakes:
  • Assigning short codes to low frequency chars
  • Ignoring prefix-free property
  • Confusing code length order
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard