Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the 2D array for dynamic programming.
DSA Java
int[][] dp = new int[[1] + 1][[2] + 1];
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping the lengths of word1 and word2.
Forgetting to add 1 to the length.
✗ Incorrect
The dp array has dimensions (word1.length() + 1) by (word2.length() + 1) to account for all prefixes including empty strings.
2fill in blank
mediumComplete the code to fill the first row of the dp array.
DSA Java
for (int j = 0; j <= word2.length(); j++) { dp[0][j] = [1]; }
Attempts:
3 left
💡 Hint
Common Mistakes
Setting dp[0][j] to 0 which ignores insertions.
Using word lengths instead of the loop variable j.
✗ Incorrect
The first row represents converting an empty string to the first j characters of word2, so dp[0][j] = j.
3fill in blank
hardFix the error in the condition to check if characters are equal.
DSA Java
if (word1.charAt(i - 1) [1] word2.charAt(j - 1)) { dp[i][j] = dp[i - 1][j - 1]; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using '!=' which reverses the logic.
Using relational operators like '>' or '<' which are invalid for char comparison here.
✗ Incorrect
We check if characters are equal using '=='. If equal, no operation is needed, so dp[i][j] = dp[i-1][j-1].
4fill in blank
hardFill both blanks to compute the minimum edit distance when characters differ.
DSA Java
dp[i][j] = 1 + Math.min(dp[i - 1][j], [1](dp[i][j - 1], [2]));
Attempts:
3 left
💡 Hint
Common Mistakes
Using Math.max instead of Math.min.
Using dp[i][j] inside the min function causing incorrect recursion.
✗ Incorrect
We use Math.min to find the smallest cost among deletion, insertion, and substitution (dp[i-1][j-1]).
5fill in blank
hardFill all three blanks to complete the nested loops for filling dp array.
DSA Java
for (int i = 1; i <= [1]; i++) { for (int j = 1; j <= [2]; j++) { if ([3]) { dp[i][j] = dp[i - 1][j - 1]; } } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong loop limits causing index errors.
Incorrect character comparison syntax.
✗ Incorrect
The outer loop runs from 1 to word1 length, inner loop from 1 to word2 length, and the condition compares characters at i-1 and j-1.
