Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare a struct for a Trie node with a children field and an is_end field.
DSA Rust
struct TrieNode {
children: Vec<Option<Box<TrieNode>>>[1]
}Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to include the is_end field.
Using a semicolon instead of a comma after the field.
✗ Incorrect
The TrieNode struct requires a 'children' field and an 'is_end: bool' field to mark the end of words. Choose the option that properly adds the is_end field with correct syntax.
2fill in blank
mediumComplete the code to initialize the children vector with None values for each child.
DSA Rust
impl TrieNode {
fn new() -> Self {
TrieNode {
children: vec![[1]; 26],
is_end: false,
}
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Using Some(...) instead of None, which creates unnecessary nodes.
Using Box::new(None) which is invalid.
✗ Incorrect
Each child is initially None, meaning no child node exists yet. So we fill the vector with None.
3fill in blank
hardFix the error in the method signature to correctly define a new TrieNode constructor.
DSA Rust
impl TrieNode {
fn new[1] -> Self {
TrieNode {
children: vec![None; 26],
is_end: false,
}
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Adding self or &self as a parameter to a static method.
Forgetting parentheses entirely.
✗ Incorrect
The new method is a static constructor and takes no parameters, so it should have empty parentheses ().
4fill in blank
hardFill both blanks to define a method that checks if a node has a child at a given index.
DSA Rust
impl TrieNode {
fn has_child(&self, index: usize) -> bool {
self.children[[1]].[2]()
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Using is_none() which returns true if no child exists.
Using len() which is not a method on Option.
✗ Incorrect
We check if the child at the given index exists by calling is_some() on the Option.
5fill in blank
hardFill all three blanks to define a method that inserts a child node at a given index.
DSA Rust
impl TrieNode {
fn insert_child(&mut self, index: usize) {
self.children[[1]] = Some(Box::new(TrieNode[2]));
self.children[[3]].as_mut().unwrap().is_end = false;
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Using a fixed index like 0 or 1 instead of the parameter.
Forgetting to call new() to create the node.
✗ Incorrect
We insert a new child at the given index by creating a new TrieNode with new(), then set is_end to false on that child.
