What does 'Count Words with Given Prefix' mean in simple terms?
It means finding how many words in a list start with a certain set of letters (prefix). For example, counting words starting with "pre" in ["prefix", "prevent", "apple"] gives 2.
Click to reveal answer
intermediate
Which data structure is commonly used to efficiently count words with a given prefix?
A Trie (prefix tree) is commonly used because it stores words by their letters and allows quick counting of words sharing the same prefix.
Click to reveal answer
intermediate
How does a Trie help in counting words with a given prefix?
Each node in a Trie can keep track of how many words pass through it. To count words with a prefix, we follow the prefix letters down the Trie and read the count stored at the last node.
Click to reveal answer
intermediate
What is the time complexity to count words with a prefix using a Trie?
It is O(k), where k is the length of the prefix, because we only follow the prefix letters down the Trie without scanning all words.
Click to reveal answer
beginner
What is a simple approach to count words with a prefix without using a Trie?
You can check each word in the list and see if it starts with the prefix. This takes O(n * k) time, where n is number of words and k is prefix length.
Click to reveal answer
What data structure is best for counting words with a given prefix efficiently?
ATrie
BStack
CQueue
DLinked List
✗ Incorrect
Trie stores words by prefixes and allows quick counting of words sharing the same prefix.
What is the time complexity to count words with prefix length k using a Trie?
AO(n)
BO(n*k)
CO(1)
DO(k)
✗ Incorrect
Counting words with a prefix in a Trie takes O(k) time, where k is the prefix length.
If you have 1000 words and prefix length 3, what is the time complexity of brute force counting?
AO(3000)
BO(3)
CO(1000)
DO(1)
✗ Incorrect
Brute force checks each word's first 3 letters, so O(n*k) = O(1000*3) = O(3000).
What does each node in a Trie typically store to help count words with a prefix?
AThe last letter only
BThe entire word
CNumber of words passing through the node
DThe word length
✗ Incorrect
Each node stores how many words pass through it to quickly count words with that prefix.
Which of these is NOT a benefit of using a Trie for prefix counting?
AFast prefix search
BEasy to implement
CSupports prefix counting in O(k)
DMemory efficient for large alphabets
✗ Incorrect
Tries can be complex to implement compared to simple loops.
Explain how a Trie data structure helps count words with a given prefix.
Think about how words share common beginnings.
You got /4 concepts.
Describe the difference between brute force and Trie-based methods for counting words with a prefix.
Compare speed and memory use.
You got /5 concepts.
Practice
(1/5)
1. What does the problem "Count Words with Given Prefix" ask you to do?
easy
A. Count how many words in a list start with a specific prefix
B. Count the total number of words in a list
C. Find the longest word in a list
D. Replace the prefix in all words with another string
Solution
Step 1: Understand the problem statement
The problem asks to find how many words start with a given prefix from a list of words.
Step 2: Identify the key operation
We need to check each word and count those that begin with the prefix.
Final Answer:
Count how many words in a list start with a specific prefix -> Option A
Quick Check:
Counting words with prefix = Count how many words in a list start with a specific prefix [OK]
Hint: Focus on words starting with prefix, not total words [OK]
Common Mistakes:
Counting all words instead of only those with prefix
Looking for longest word instead of counting
Replacing prefix instead of counting
2. Which of the following C++ code snippets correctly checks if a string word starts with prefix pref?
easy
A. if (word.compare(pref) == 1) { /* starts with prefix */ }
B. if (word == pref) { /* starts with prefix */ }
C. if (word.substr(1, pref.size()) == pref) { /* starts with prefix */ }
D. if (word.find(pref) == 0) { /* starts with prefix */ }
Solution
Step 1: Understand string::find usage
Using word.find(pref) == 0 checks if pref is found at the start (index 0) of word.
Step 2: Check other options
if (word == pref) { /* starts with prefix */ } checks equality, not prefix. if (word.substr(1, pref.size()) == pref) { /* starts with prefix */ } checks substring starting at index 1, which is wrong. if (word.compare(pref) == 1) { /* starts with prefix */ } compares strings but result 1 means greater, not prefix.
Final Answer:
if (word.find(pref) == 0) { /* starts with prefix */ } -> Option D
Quick Check:
find(pref) == 0 means prefix match [OK]
Hint: Use find(pref) == 0 to check prefix in C++ strings [OK]
Common Mistakes:
Using equality instead of prefix check
Checking substring from wrong index
Misunderstanding compare() return values
3. What is the output of the following C++ code?
std::vector words = {"apple", "app", "application", "banana"};
std::string prefix = "app";
int count = 0;
for (const auto& w : words) {
if (w.find(prefix) == 0) {
count++;
}
}
std::cout << count << std::endl;
medium
A. 3
B. 2
C. 1
D. 4
Solution
Step 1: Check each word for prefix "app"
"apple" starts with "app" (count=1), "app" starts with "app" (count=2), "application" starts with "app" (count=3), "banana" does not.
Step 2: Count total matches
Total words starting with "app" are 3.
Final Answer:
3 -> Option A
Quick Check:
Words starting with "app" = 3 [OK]
Hint: Count words where find(prefix) == 0 inside loop [OK]
Common Mistakes:
Counting words that contain prefix anywhere
Off-by-one errors in counting
Ignoring words that exactly equal prefix
4. Identify the error in this C++ code snippet that tries to count words starting with prefix pref:
int count = 0;
for (auto w : words) {
if (w.find(pref)) {
count++;
}
}
medium
A. The count variable should be initialized to -1
B. The loop variable should be a reference: auto& w
C. The condition should check if w.find(pref) == 0, not just w.find(pref)
D. The find function should be replaced with substr
Solution
Step 1: Understand find() return value
find() returns position or npos. Using if(w.find(pref)) treats 0 as false, so words starting at index 0 are skipped.
Step 2: Correct condition for prefix check
We must check if w.find(pref) == 0 to confirm prefix at start.
Final Answer:
The condition should check if w.find(pref) == 0, not just w.find(pref) -> Option C
Quick Check:
Check find() == 0 for prefix [OK]
Hint: Always compare find() result to 0 for prefix check [OK]
Common Mistakes:
Using if(w.find(pref)) which fails for prefix at start
Not using reference in loop (less critical here)
Initializing count incorrectly
5. Given a list of words and a prefix, how would you efficiently count words starting with the prefix if the list is very large and queried multiple times? Choose the best approach.
hard
A. Check each word with find(prefix) == 0 every time a query comes
B. Use a Trie (prefix tree) to store words and count prefix matches quickly
C. Sort the list and use binary search to find prefix range each time
D. Store words in a hash set and check prefix membership
Solution
Step 1: Understand repeated prefix queries
Checking each word every time is slow for large lists and many queries.
Step 2: Use Trie data structure
A Trie stores words by prefixes, allowing fast counting of words with given prefix in O(length of prefix) time.
Step 3: Evaluate other options
Binary search after sorting helps but is less efficient for many queries. Hash set doesn't support prefix queries efficiently.
Final Answer:
Use a Trie (prefix tree) to store words and count prefix matches quickly -> Option B
Quick Check:
Trie = fast prefix counting [OK]
Hint: Use Trie for fast repeated prefix counts [OK]
Common Mistakes:
Checking all words each query (slow)
Using hash set which doesn't support prefix queries
Ignoring Trie advantages for prefix problems
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.