Challenge - 5 Problems
Trie Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Trie Node Initialization in Rust
What is the output of the following Rust code that initializes a Trie node and prints its children count?
DSA Rust
use std::collections::HashMap;
struct TrieNode {
children: HashMap<char, TrieNode>,
is_end_of_word: bool,
}
impl TrieNode {
fn new() -> Self {
TrieNode {
children: HashMap::new(),
is_end_of_word: false,
}
}
}
fn main() {
let root = TrieNode::new();
println!("{}", root.children.len());
}Attempts:
2 left
💡 Hint
Think about what the children HashMap contains right after initialization.
✗ Incorrect
The TrieNode is initialized with an empty HashMap for children, so its length is 0.
❓ Predict Output
intermediateResult of Adding a Child Node to Trie
What will be the output after adding a child node for character 'a' to the root Trie node and printing the children count?
DSA Rust
use std::collections::HashMap;
struct TrieNode {
children: HashMap<char, TrieNode>,
is_end_of_word: bool,
}
impl TrieNode {
fn new() -> Self {
TrieNode {
children: HashMap::new(),
is_end_of_word: false,
}
}
fn add_child(&mut self, ch: char) {
self.children.insert(ch, TrieNode::new());
}
}
fn main() {
let mut root = TrieNode::new();
root.add_child('a');
println!("{}", root.children.len());
}Attempts:
2 left
💡 Hint
Adding a child inserts one entry into the children HashMap.
✗ Incorrect
After adding a child node for 'a', the children HashMap contains one entry, so length is 1.
🧠 Conceptual
advancedUnderstanding Ownership in Trie Node Children
In Rust, why is it important that the children of a TrieNode are stored as HashMap instead of HashMap?
Attempts:
2 left
💡 Hint
Think about Rust's ownership and borrowing rules.
✗ Incorrect
Storing owned TrieNodes avoids lifetime issues and dangling references, which are complex to manage with references.
🔧 Debug
advancedIdentify the Error in TrieNode Initialization
What error will this Rust code produce when compiling?
DSA Rust
use std::collections::HashMap;
struct TrieNode {
children: HashMap<char, TrieNode>,
is_end_of_word: bool,
}
impl TrieNode {
fn new() -> Self {
TrieNode {
children: HashMap::new,
is_end_of_word: false,
}
}
}
fn main() {
let root = TrieNode::new();
}Attempts:
2 left
💡 Hint
Check how functions are called in Rust.
✗ Incorrect
HashMap::new is a function and must be called with parentheses (). Missing them causes a syntax error.
🚀 Application
expertPredict the Trie Structure After Insertions
Given the following insertions into an initially empty Trie, what is the printed output of the root's children keys in sorted order?
DSA Rust
use std::collections::HashMap;
struct TrieNode {
children: HashMap<char, TrieNode>,
is_end_of_word: bool,
}
impl TrieNode {
fn new() -> Self {
TrieNode {
children: HashMap::new(),
is_end_of_word: false,
}
}
fn insert(&mut self, word: &str) {
let mut current = self;
for ch in word.chars() {
current = current.children.entry(ch).or_insert(TrieNode::new());
}
current.is_end_of_word = true;
}
}
fn main() {
let mut root = TrieNode::new();
root.insert("cat");
root.insert("car");
root.insert("dog");
let mut keys: Vec<char> = root.children.keys().cloned().collect();
keys.sort();
for k in keys {
print!("{} ", k);
}
}Attempts:
2 left
💡 Hint
Only the first letters of inserted words appear as root children keys.
✗ Incorrect
The root children keys are the first letters of inserted words: 'c' from "cat" and "car", and 'd' from "dog".
