Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the prefix array with zeros.
DSA C++
int prefix[n] = { [1] };Attempts:
3 left
💡 Hint
Common Mistakes
Initializing prefix array with ones or negative values.
Not initializing the prefix array at all.
✗ Incorrect
The prefix array should be initialized with zeros to start building the longest prefix suffix values.
2fill in blank
mediumComplete the code to update the prefix array when characters match.
DSA C++
if (pattern[i] == pattern[j]) { j++; prefix[i] = [1]; i++; }
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning prefix[i] to i instead of j.
Assigning zero instead of the updated prefix length.
✗ Incorrect
When characters match, prefix[i] is set to j, the length of the current longest prefix suffix.
3fill in blank
hardFix the error in the while loop condition for building the prefix array.
DSA C++
while (i < n) { if (pattern[i] == pattern[j]) { j++; prefix[i] = j; i++; } else { if ([1] != 0) { j = prefix[j - 1]; } else { prefix[i] = 0; i++; } } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using i instead of j in the condition.
Using prefix[i] or prefix[j] incorrectly.
✗ Incorrect
The condition checks if j is not zero to fallback to the previous longest prefix suffix.
4fill in blank
hardFill both blanks to correctly update indices during the search phase.
DSA C++
while (i < n) { if (text[i] == pattern[j]) { i++; j = [1]; } else { if (j != 0) { j = [2]; } else { i++; } } }
Attempts:
3 left
💡 Hint
Common Mistakes
Not incrementing j on match.
Incorrect fallback index on mismatch.
✗ Incorrect
When characters match, j increments by 1; when mismatch occurs and j is not zero, j falls back to prefix[j-1].
5fill in blank
hardFill all three blanks to correctly detect pattern matches and reset j.
DSA C++
if (j == [1]) { result.push_back(i - j); j = [2]; } // Build prefix array int prefix[n] = {0}; int i = 1, j = 0; while (i < [3]) { // prefix array logic }
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong length variables for comparisons.
Not resetting j correctly after a match.
✗ Incorrect
j equals pattern length m means a full match; reset j to prefix[j-1]; prefix array is built for pattern length n.
