Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to get the length of the pattern string.
DSA C++
int m = [1].length();Attempts:
3 left
💡 Hint
Common Mistakes
Using the length of the text instead of the pattern.
Using an undefined variable.
✗ Incorrect
We need the length of the pattern string to know how many characters to compare.
2fill in blank
mediumComplete the code to loop through the text string for possible pattern matches.
DSA C++
for (int i = 0; i <= n - [1]; i++) {
Attempts:
3 left
💡 Hint
Common Mistakes
Looping until n instead of n - m.
Using the wrong variable in the loop condition.
✗ Incorrect
We loop until the last possible start index where the pattern can fit in the text, which is n - m.
3fill in blank
hardFix the error in the inner loop condition to compare characters correctly.
DSA C++
for (int j = 0; j < [1]; j++) {
Attempts:
3 left
💡 Hint
Common Mistakes
Using the text length n instead of pattern length m.
Using the outer loop variable i inside the inner loop condition.
✗ Incorrect
The inner loop should run for the length of the pattern (m) to compare each character.
4fill in blank
hardFill both blanks to check if characters match and break if they don't.
DSA C++
if (text[i + [1]] != pattern[[2]]) { break; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variables for text and pattern indices.
Using outer loop variable i in pattern index.
✗ Incorrect
We compare the character at text index i + j with pattern index j. If they differ, we break.
5fill in blank
hardFill all three blanks to print the match index when pattern is found.
DSA C++
if (j == [1]) { std::cout << "Pattern found at index " << [2] << std::endl; [3] = true; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong variables for comparison or printing.
Not setting the found flag correctly.
✗ Incorrect
When j equals m, the whole pattern matched. We print the index i and set found to true.
