Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to check if a word starts with the given prefix.
DSA C++
bool startsWith(const string& word, const string& prefix) {
return word.[1](0, prefix.size()) == prefix;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using find instead of substr returns the position, not the substring.
Using compare returns an int, not a string.
✗ Incorrect
The substr function extracts a substring from the word starting at index 0 with length equal to prefix size, which is then compared to the prefix.
2fill in blank
mediumComplete the code to count how many words start with the given prefix.
DSA C++
int countPrefix(const vector<string>& words, const string& prefix) {
int count = 0;
for (const auto& word : words) {
if (word.size() >= prefix.size() && word.[1](0, prefix.size()) == prefix) {
count++;
}
}
return count;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using find returns position, which is not suitable for prefix check.
Using compare returns int, not string.
✗ Incorrect
Using substr to extract the prefix part of each word allows comparison with the given prefix to count matches.
3fill in blank
hardFix the error in the code to correctly count words starting with the prefix.
DSA C++
int countPrefix(const vector<string>& words, const string& prefix) {
int count = 0;
for (const auto& word : words) {
if (word.size() >= prefix.size() && word.[1](0, prefix.size(), prefix) == 0) {
count++;
}
}
return count;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using find returns position, not 0 for match.
Using substr returns string, cannot be compared to 0.
✗ Incorrect
The compare function can compare the prefix with the start of the word and returns 0 if they match.
4fill in blank
hardFill both blanks to create a map of prefix counts for given words.
DSA C++
unordered_map<string, int> prefixCount(const vector<string>& words, int prefixLen) {
unordered_map<string, int> counts;
for (const auto& word : words) {
if (word.size() >= [1]) {
string prefix = word.[2](0, prefixLen);
counts[prefix]++;
}
}
return counts;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using find instead of substr to extract prefix.
Checking word.size() < prefixLen instead of >=.
✗ Incorrect
Check if word length is at least prefixLen, then extract prefix using substr from index 0 with length prefixLen.
5fill in blank
hardFill all three blanks to return the count of words starting with prefix using a loop and condition.
DSA C++
int countWordsWithPrefix(const vector<string>& words, const string& prefix) {
int count = 0;
for (const auto& [1] : words) {
if ([2].[3](0, prefix.size()) == prefix) {
count++;
}
}
return count;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'words' as loop variable instead of 'word'.
Using find instead of substr for prefix extraction.
✗ Incorrect
Use 'word' as loop variable, then call word.substr(0, prefix.size()) to compare with prefix.