Challenge - 5 Problems
Prefix Count Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of prefix count after inserting words
What is the output of the Rust code that inserts words into a prefix count structure and queries the count for prefix "app"?
DSA Rust
struct TrieNode {
children: std::collections::HashMap<char, TrieNode>,
count: usize,
}
impl TrieNode {
fn new() -> Self {
TrieNode {
children: std::collections::HashMap::new(),
count: 0,
}
}
fn insert(&mut self, word: &str) {
let mut node = self;
for ch in word.chars() {
node = node.children.entry(ch).or_insert_with(TrieNode::new);
node.count += 1;
}
}
fn count_prefix(&self, prefix: &str) -> usize {
let mut node = self;
for ch in prefix.chars() {
if let Some(next) = node.children.get(&ch) {
node = next;
} else {
return 0;
}
}
node.count
}
}
fn main() {
let mut root = TrieNode::new();
root.insert("apple");
root.insert("app");
root.insert("application");
root.insert("apt");
println!("{}", root.count_prefix("app"));
}Attempts:
2 left
💡 Hint
Count how many inserted words start with "app".
✗ Incorrect
The words "apple", "app", and "application" all start with "app", so the count is 3.
🧠 Conceptual
intermediateUnderstanding prefix count in a trie
If a trie stores the words ["cat", "car", "cart", "dog"], what is the count of words with prefix "car"?
Attempts:
2 left
💡 Hint
Count words starting exactly with "car" prefix.
✗ Incorrect
The words "car" and "cart" start with "car", so the count is 2.
🔧 Debug
advancedIdentify the error in prefix count code
What error does this Rust code produce when counting prefix "ba" after inserting words "bat" and "ball"?
Code snippet:
let mut root = TrieNode::new();
root.insert("bat");
root.insert("ball");
println!("{}", root.count_prefix("ba"));
struct TrieNode {
children: std::collections::HashMap,
count: usize,
}
impl TrieNode {
fn new() -> Self {
TrieNode {
children: std::collections::HashMap::new(),
count: 0,
}
}
fn insert(&mut self, word: &str) {
let mut node = self;
for ch in word.chars() {
node = node.children.entry(ch).or_insert_with(TrieNode::new);
// Missing count increment here
}
}
fn count_prefix(&self, prefix: &str) -> usize {
let mut node = self;
for ch in prefix.chars() {
if let Some(next) = node.children.get(&ch) {
node = next;
} else {
return 0;
}
}
node.count
}
}
Attempts:
2 left
💡 Hint
Check if counts are updated during insertion.
✗ Incorrect
The count field is never incremented during insertion, so it remains zero for all nodes. Thus, count_prefix returns 0.
❓ Predict Output
advancedOutput after multiple prefix queries
What is the output of the following Rust code printing counts for prefixes "a", "ap", "app", and "b"?
DSA Rust
struct TrieNode {
children: std::collections::HashMap<char, TrieNode>,
count: usize,
}
impl TrieNode {
fn new() -> Self {
TrieNode {
children: std::collections::HashMap::new(),
count: 0,
}
}
fn insert(&mut self, word: &str) {
let mut node = self;
for ch in word.chars() {
node = node.children.entry(ch).or_insert_with(TrieNode::new);
node.count += 1;
}
}
fn count_prefix(&self, prefix: &str) -> usize {
let mut node = self;
for ch in prefix.chars() {
if let Some(next) = node.children.get(&ch) {
node = next;
} else {
return 0;
}
}
node.count
}
}
fn main() {
let mut root = TrieNode::new();
root.insert("apple");
root.insert("app");
root.insert("ape");
root.insert("bat");
println!("{} {} {} {}", root.count_prefix("a"), root.count_prefix("ap"), root.count_prefix("app"), root.count_prefix("b"));
}Attempts:
2 left
💡 Hint
Count words starting with each prefix carefully.
✗ Incorrect
Words starting with "a" are "apple", "app", "ape" (3 words). But count includes nodes, so counts are cumulative: root->a (4), a->p (3), ap->p (2), b (1).
🚀 Application
expertNumber of words with prefix after deletions
Given a trie that supports insertions and deletions, after inserting words ["test", "team", "teach", "tear"] and deleting "team", what is the count of words with prefix "te"?
DSA Rust
struct TrieNode {
children: std::collections::HashMap<char, TrieNode>,
count: usize,
end_count: usize,
}
impl TrieNode {
fn new() -> Self {
TrieNode {
children: std::collections::HashMap::new(),
count: 0,
end_count: 0,
}
}
fn insert(&mut self, word: &str) {
let mut node = self;
for ch in word.chars() {
node = node.children.entry(ch).or_insert_with(TrieNode::new);
node.count += 1;
}
node.end_count += 1;
}
fn delete(&mut self, word: &str) -> bool {
if !self._delete(word, 0) {
return false;
}
true
}
fn _delete(&mut self, word: &str, index: usize) -> bool {
if index == word.len() {
if self.end_count == 0 {
return false;
}
self.end_count -= 1;
self.count -= 1;
return true;
}
let ch = word.chars().nth(index).unwrap();
if let Some(child) = self.children.get_mut(&ch) {
if child._delete(word, index + 1) {
self.count -= 1;
if child.count == 0 {
self.children.remove(&ch);
}
return true;
}
}
false
}
fn count_prefix(&self, prefix: &str) -> usize {
let mut node = self;
for ch in prefix.chars() {
if let Some(next) = node.children.get(&ch) {
node = next;
} else {
return 0;
}
}
node.count
}
}
fn main() {
let mut root = TrieNode::new();
root.insert("test");
root.insert("team");
root.insert("teach");
root.insert("tear");
root.delete("team");
println!("{}", root.count_prefix("te"));
}Attempts:
2 left
💡 Hint
Count words starting with "te" after removing "team".
✗ Incorrect
After deleting "team", the words with prefix "te" are "test", "teach", and "tear", so count is 3.
