Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the DP matrix with zeros.
DSA Rust
let mut dp = vec![vec![[1]; n + 1]; m + 1];
Attempts:
3 left
💡 Hint
Common Mistakes
Using 1 instead of 0 causes incorrect base initialization.
Using None or empty vectors causes type errors.
✗ Incorrect
The DP matrix should be initialized with zeros to represent base cases before filling in edit distances.
2fill in blank
mediumComplete the code to fill the first row of the DP matrix with increasing values.
DSA Rust
for j in 0..=n { dp[0][j] = [1]; }
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning 0 to all cells causes wrong distances.
Using m or n instead of j ignores the loop index.
✗ Incorrect
The first row represents converting empty string to prefix of second string, so distance equals j.
3fill in blank
hardFix the error in the condition to check if characters are equal.
DSA Rust
if s1.chars().nth(i - 1) [1] s2.chars().nth(j - 1) { dp[i][j] = dp[i - 1][j - 1]; } else { dp[i][j] = 1 + std::cmp::min( dp[i - 1][j - 1], std::cmp::min(dp[i][j - 1], dp[i - 1][j]) ); }
Attempts:
3 left
💡 Hint
Common Mistakes
Using assignment '=' causes syntax error.
Using '!=' reverses logic and gives wrong results.
✗ Incorrect
We check if characters are equal using '=='. If equal, no edit needed, so copy previous value.
4fill in blank
hardFill both blanks to correctly compute the minimum edit distance.
DSA Rust
dp[i][j] = 1 + std::cmp::min(dp[i - 1][j - 1], std::cmp::min(dp[[1]][j], dp[i][[2]]));
Attempts:
3 left
💡 Hint
Common Mistakes
Using i+1 or j+1 causes index out of bounds errors.
Mixing row and column indices incorrectly.
✗ Incorrect
We compare substitution (i-1,j-1), deletion (i-1,j), and insertion (i,j-1) costs to find minimum.
5fill in blank
hardFill all three blanks to complete the function returning the edit distance.
DSA Rust
fn levenshtein(s1: &str, s2: &str) -> usize {
let m = s1.len();
let n = s2.len();
let mut dp = vec![vec![0; n + 1]; m + 1];
for i in 0..=m {
dp[i][0] = i;
}
for j in 0..=n {
dp[0][j] = j;
}
for i in 1..=m {
for j in 1..=n {
if s1.chars().nth(i - 1) [1] s2.chars().nth(j - 1) {
dp[i][j] = dp[i - 1][j - 1];
} else {
dp[i][j] = 1 + std::cmp::min(
dp[i - 1][j - 1],
std::cmp::min(dp[i - 1][j], dp[i][j - 1])
);
}
}
}
dp[[2]][[3]]
}Attempts:
3 left
💡 Hint
Common Mistakes
Using '!=' reverses logic and causes wrong results.
Returning dp[0][0] or dp[n][m] gives incorrect answer.
✗ Incorrect
Use '==' to compare characters. The final answer is at dp[m][n], the full lengths of both strings.
