What does the problem 'Count Words with Given Prefix' ask you to do?
It asks to count how many words in a list start with a specific prefix string.
Click to reveal answer
intermediate
Why is a Trie (prefix tree) useful for counting words with a given prefix?
A Trie stores words so that common prefixes share nodes, making it fast to find and count words starting with a prefix.
Click to reveal answer
intermediate
In Rust, what data structure can efficiently represent children nodes in a Trie?
A HashMap can be used to map each character to its child node.
Click to reveal answer
intermediate
What is the time complexity to count words with a given prefix using a Trie?
It is O(m), where m is the length of the prefix, because you only traverse nodes matching the prefix.
Click to reveal answer
beginner
What is a simple way to count words with a prefix without a Trie?
You can iterate over all words and check if each starts with the prefix, but this is slower (O(n * m)).
Click to reveal answer
What data structure is best suited for prefix-based word counting?
AStack
BQueue
CTrie
DLinked List
✗ Incorrect
Trie efficiently stores words by prefixes, making prefix queries fast.
What does the prefix length affect in a Trie search?
AThe number of nodes traversed
BThe total number of words stored
CThe size of the Trie
DThe number of children per node
✗ Incorrect
You only traverse nodes equal to the prefix length to find matching words.
In Rust, which collection is commonly used to map characters to child nodes in a Trie?
AHashMap<char, TrieNode>
BVec<char>
CBTreeSet<char>
DLinkedList<char>
✗ Incorrect
HashMap allows fast lookup of child nodes by character.
What is the brute force approach to count words with a prefix?
ASort words and binary search
BUse a Trie to store words
CUse a stack to store prefixes
DCheck each word if it starts with the prefix
✗ Incorrect
Brute force checks each word one by one for the prefix.
What is the main advantage of using a Trie over brute force for prefix counting?
ALess memory usage
BFaster prefix queries
CSimpler code
DNo need to store words
✗ Incorrect
Trie reduces prefix query time from O(n*m) to O(m).
Explain how a Trie helps count words with a given prefix efficiently.
Think about how words with the same start share nodes.
You got /4 concepts.
Describe the brute force method to count words with a prefix and its drawbacks.
Consider checking each word one by one.
You got /4 concepts.
Practice
(1/5)
1. What does the problem "Count Words with Given Prefix" ask you to do?
easy
A. Find the longest word in a list
B. Count how many words in a list start with a specific prefix
C. Count the total number of characters in all words
D. Sort the words alphabetically
Solution
Step 1: Understand the problem statement
The problem asks to find how many words start with a given prefix, not about length or sorting.
Step 2: Identify the key task
Counting words that begin with the prefix is the main task.
Final Answer:
Count how many words in a list start with a specific prefix -> Option B
Quick Check:
Count words starting with prefix = Count how many words in a list start with a specific prefix [OK]
Hint: Focus on words starting with prefix, not full word or sorting [OK]
Common Mistakes:
Confusing prefix with substring anywhere in word
Counting total characters instead of words
Sorting words instead of counting
2. Which of the following Rust code snippets correctly checks if a word starts with a given prefix prefix?
easy
A. word.starts_with(prefix)
B. word.contains(prefix)
C. word.ends_with(prefix)
D. word == prefix
Solution
Step 1: Recall Rust string methods
Rust's starts_with method checks if a string begins with a given substring.
Step 2: Compare options
contains checks anywhere, ends_with checks suffix, and == checks full equality, so only starts_with fits.
Final Answer:
word.starts_with(prefix) -> Option A
Quick Check:
Use starts_with() to check prefix [OK]
Hint: Use starts_with() method to check prefix in Rust strings [OK]
Common Mistakes:
Using contains() which matches anywhere, not just prefix
Using ends_with() which checks suffix
Comparing full equality instead of prefix
3. What is the output of this Rust code?
let words = vec!["apple", "app", "application", "banana"];
let prefix = "app";
let count = words.iter().filter(|w| w.starts_with(prefix)).count();
println!("{}", count);
medium
A. 3
B. 2
C. 1
D. 4
Solution
Step 1: Identify words starting with "app"
Words are "apple", "app", "application", and "banana". The first three start with "app".
Step 2: Count matching words
Count is 3 because "apple", "app", and "application" start with "app".
Final Answer:
3 -> Option A
Quick Check:
3 words start with "app" [OK]
Hint: Count words where starts_with(prefix) is true [OK]
Common Mistakes:
Counting only exact matches
Including words that don't start with prefix
Off-by-one counting errors
4. The following Rust code tries to count words starting with a prefix but gives wrong output. What is the bug?
let words = vec!["cat", "car", "dog", "scare"];
let prefix = "car";
let count = words.iter().filter(|w| w.contains(prefix)).count();
println!("{}", count);
medium
A. Missing semicolon after count declaration
B. Prefix variable is not declared correctly
C. The vector words is empty
D. Using contains() instead of starts_with() causes wrong matches
Solution
Step 1: Understand difference between contains() and starts_with()
contains() checks if prefix appears anywhere, not just at start.
Step 2: Identify incorrect matches
"scare" contains "car" but does not start with "car", so contains() wrongly counts it.
Final Answer:
Using contains() instead of starts_with() causes wrong matches -> Option D
Quick Check:
Use starts_with() to count prefix matches [OK]
Hint: Replace contains() with starts_with() to fix prefix counting [OK]
Common Mistakes:
Confusing contains() with starts_with()
Assuming contains() only matches prefix
Ignoring method differences
5. Given a list of words ["test", "tester", "testing", "toast", "team"] and prefix "te", which Rust code correctly counts words starting with the prefix and ignores case sensitivity?
hard
A. words.iter().filter(|w| w.contains(prefix)).count()
B. words.iter().filter(|w| w.starts_with(prefix)).count()
C. words.iter().filter(|w| w.to_lowercase().starts_with(&prefix.to_lowercase())).count()
D. words.iter().filter(|w| w.ends_with(prefix)).count()
Solution
Step 1: Understand case sensitivity in Rust strings
Rust's starts_with is case sensitive, so we convert both word and prefix to lowercase.
Step 2: Check each option for case-insensitive prefix check
words.iter().filter(|w| w.to_lowercase().starts_with(&prefix.to_lowercase())).count() converts both to lowercase before checking prefix, ensuring case-insensitive match. Others do not handle case.
Final Answer:
words.iter().filter(|w| w.to_lowercase().starts_with(&prefix.to_lowercase())).count() -> Option C
Quick Check:
Convert both strings to lowercase before starts_with() [OK]
Hint: Lowercase both word and prefix before starts_with() for case-insensitive [OK]
Common Mistakes:
Ignoring case sensitivity in starts_with()
Using contains() which is wrong for prefix
Using ends_with() instead of starts_with()
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.