What does the problem 'Count Words with Given Prefix' ask you to do?
It asks you to count how many words in a list start with a specific prefix string.
Click to reveal answer
beginner
Which JavaScript string method helps check if a word starts with a given prefix?
The startsWith() method returns true if a string starts with the specified prefix.
Click to reveal answer
beginner
How can you count words with a prefix using a simple loop in JavaScript?
Loop through each word, use startsWith(prefix) to check, and increase a counter if true.
Click to reveal answer
intermediate
What is a Trie and how can it help in counting words with a prefix?
A Trie is a tree-like data structure that stores words by their letters. It can quickly find how many words share a prefix by traversing the prefix path.
Click to reveal answer
intermediate
Why is using a Trie more efficient than checking each word individually for large datasets?
Because a Trie stores common prefixes once, it avoids repeated checks and can count prefix matches faster than scanning all words.
Click to reveal answer
Which JavaScript method checks if a string starts with a specific prefix?
AstartsWith()
Bincludes()
CindexOf()
Dsubstring()
✗ Incorrect
The startsWith() method returns true if the string begins with the given prefix.
What is the main task in 'Count Words with Given Prefix'?
ACount words that start with the prefix
BCount words that contain the prefix anywhere
CCount words that end with the prefix
DCount words that have the prefix in the middle
✗ Incorrect
The problem requires counting words that start exactly with the given prefix.
Which data structure is best for efficient prefix searches?
AStack
BTrie
CArray
DQueue
✗ Incorrect
A Trie stores words by prefixes, making prefix searches very efficient.
If you have 1000 words and want to count prefix matches, what is a simple approach?
ASort the words alphabetically
BReverse each word and check suffix
CUse a stack to store words
DUse a loop and startsWith() for each word
✗ Incorrect
Looping through each word and checking with startsWith() is a straightforward method.
What does the startsWith() method return if the prefix is empty string?
Afalse
Bundefined
Ctrue
Dthrows error
✗ Incorrect
An empty string is considered a prefix of any string, so startsWith('') returns true.
Explain how you would count words starting with a given prefix using JavaScript.
Think about checking each word one by one.
You got /3 concepts.
Describe what a Trie is and how it helps with prefix counting.
Imagine a tree where each branch is a letter.
You got /4 concepts.
Practice
(1/5)
1. What does the function countWordsWithPrefix(words, prefix) do when given a list of words and a prefix?
easy
A. Counts how many words contain the prefix anywhere
B. Counts the total number of words in the list
C. Counts how many words end with the given prefix
D. Counts how many words start with the given prefix
Solution
Step 1: Understand the function purpose
The function checks each word to see if it starts with the given prefix.
Step 2: Count matching words
It counts only those words that begin exactly with the prefix, ignoring others.
Final Answer:
Counts how many words start with the given prefix -> Option D
Quick Check:
Starts with prefix = Count [OK]
Hint: Look for words starting exactly with prefix [OK]
Common Mistakes:
Counting words containing prefix anywhere
Counting words ending with prefix
Counting total words instead of filtered ones
2. Which of the following is the correct syntax to check if a string word starts with a prefix prefix in JavaScript?
easy
A. word.beginWith(prefix)
B. word.hasPrefix(prefix)
C. word.startsWith(prefix)
D. word.starts(prefix)
Solution
Step 1: Recall JavaScript string methods
JavaScript strings have a method called startsWith() to check prefix.
Step 2: Match method name
Only startsWith() is the correct method; others do not exist.
Final Answer:
word.startsWith(prefix) -> Option C
Quick Check:
Correct method name = startsWith [OK]
Hint: Use startsWith() method for prefix check [OK]
Common Mistakes:
Using non-existent methods like hasPrefix
Using incorrect method names like beginWith
Confusing startsWith with other string methods
3. What is the output of the following code?
const words = ['apple', 'app', 'application', 'banana'];
const prefix = 'app';
function countWordsWithPrefix(words, prefix) {
let count = 0;
for (const word of words) {
if (word.startsWith(prefix)) {
count++;
}
}
return count;
}
console.log(countWordsWithPrefix(words, prefix));
medium
A. 3
B. 2
C. 1
D. 4
Solution
Step 1: Check each word against prefix 'app'
'apple', 'app', and 'application' all start with 'app'. 'banana' does not.
Step 2: Count matching words
There are 3 words starting with 'app', so count is 3.
Final Answer:
3 -> Option A
Quick Check:
Words starting with 'app' = 3 [OK]
Hint: Count words starting exactly with prefix [OK]
Common Mistakes:
Counting words containing prefix anywhere
Missing one of the matching words
Counting all words regardless of prefix
4. The following code is intended to count words starting with a prefix, but it has a bug. What is the bug?
function countWordsWithPrefix(words, prefix) {
let count = 0;
for (let i = 0; i <= words.length; i++) {
if (words[i].startsWith(prefix)) {
count++;
}
}
return count;
}
medium
A. startsWith method is used incorrectly
B. Loop goes out of bounds causing an error
C. count variable is not initialized
D. Prefix is not passed as a parameter
Solution
Step 1: Analyze loop condition
The loop runs with i from 0 to words.length inclusive, but array indices go from 0 to words.length - 1.
Step 2: Identify out of bounds access
At i = words.length, words[i] is undefined, so calling startsWith causes an error.
Final Answer:
Loop goes out of bounds causing an error -> Option B
Quick Check:
Loop index must be less than length [OK]
Hint: Use i < words.length, not <= [OK]
Common Mistakes:
Using <= instead of < in loop condition
Not checking array bounds before access
Assuming startsWith works on undefined
5. Given the array words = ['Test', 'testing', 'Tester', 'tent', 'team'] and prefix 'te', which code snippet correctly counts words starting with the prefix ignoring case sensitivity?
hard
A. words.filter(w => w.toLowerCase().startsWith(prefix.toLowerCase())).length
B. words.filter(w => w.startsWith(prefix)).length
C. words.filter(w => w.includes(prefix)).length
D. words.filter(w => w.endsWith(prefix)).length
Solution
Step 1: Understand case sensitivity issue
Direct startsWith is case sensitive, so 'Test' and 'test' differ.
Step 2: Use toLowerCase() to normalize
Convert both word and prefix to lowercase before checking startsWith to ignore case.
Final Answer:
words.filter(w => w.toLowerCase().startsWith(prefix.toLowerCase())).length -> Option A
Quick Check:
Normalize case before prefix check [OK]
Hint: Convert both strings to lowercase before startsWith [OK]
Common Mistakes:
Ignoring case sensitivity
Using includes instead of startsWith
Using endsWith instead of startsWith
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.