Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the prefix array with zeros.
DSA Rust
let mut prefix = vec![[1]; pattern.len()];
Attempts:
3 left
💡 Hint
Common Mistakes
Initializing prefix array with ones or negative numbers.
Using pattern length as initial value.
✗ Incorrect
The prefix array should be initialized with zeros to store the longest prefix lengths.
2fill in blank
mediumComplete the code to update the prefix length when characters match.
DSA Rust
if pattern[i] == pattern[j] { j += 1; prefix[i] = [1]; i += 1; }
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning prefix[i] to i instead of j.
Using zero or unrelated values.
✗ Incorrect
When characters match, prefix[i] is set to the current prefix length j.
3fill in blank
hardFix the error in the while loop condition to avoid index out of bounds.
DSA Rust
while i < [1] { // loop body }
Attempts:
3 left
💡 Hint
Common Mistakes
Using text length instead of pattern length.
Using prefix length which might be same but less clear.
✗ Incorrect
The prefix computation loop runs over the pattern length, so use pattern.len().
4fill in blank
hardFill both blanks to correctly update indices during pattern matching.
DSA Rust
if pattern[j] == text[i] { i += [1]; j += [2]; } else if j != 0 { j = prefix[j - 1]; } else { i += 1; }
Attempts:
3 left
💡 Hint
Common Mistakes
Incrementing only one index.
Using zero or negative increments.
✗ Incorrect
When characters match, both indices i and j move forward by 1.
5fill in blank
hardFill all three blanks to correctly check for pattern match and record the index.
DSA Rust
if j == [1] { result.push(i - j + [2]); j = prefix[j - [3]]; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using text length instead of pattern length.
Incorrect index calculation for match start.
Wrong prefix index for resetting j.
✗ Incorrect
When j equals pattern length, a match is found. The start index is i - j + 1, then j resets using prefix[j-1].
