Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the base case for the DP table.
DSA C++
for (int i = 0; i <= m; i++) dp[i][0] = [1];
Attempts:
3 left
💡 Hint
Common Mistakes
Setting dp[i][0] to 0 instead of i.
Using the wrong variable like m or n instead of i.
✗ Incorrect
The base case for transforming a string of length i to an empty string requires i deletions, so dp[i][0] = i.
2fill in blank
mediumComplete the code to check if characters at position i-1 and j-1 are equal.
DSA C++
if (word1[i - 1] [1] word2[j - 1]) dp[i][j] = dp[i - 1][j - 1];
Attempts:
3 left
💡 Hint
Common Mistakes
Using '!=' instead of '=='.
Using comparison operators like '>' or '<' which are incorrect here.
✗ Incorrect
If the characters are equal, no operation is needed, so dp[i][j] equals dp[i-1][j-1].
3fill in blank
hardFix the error in the code to compute the minimum edit distance with insert, delete, and replace operations.
DSA C++
dp[i][j] = 1 + std::min({dp[i - 1][j], dp[i][j - 1], [1]);
Attempts:
3 left
💡 Hint
Common Mistakes
Using dp[i][j] which causes infinite recursion.
Using indices out of bounds like dp[i-1][j+1].
✗ Incorrect
The replace operation cost is dp[i-1][j-1], so it must be included in the minimum calculation.
4fill in blank
hardFill both blanks to complete the nested loops iterating over the DP table.
DSA C++
for (int [1] = 1; [1] <= m; [1]++) { for (int [2] = 1; [2] <= n; [2]++) {
Attempts:
3 left
💡 Hint
Common Mistakes
Using the same variable name for both loops.
Using uncommon variable names that confuse readers.
✗ Incorrect
The standard variable names for rows and columns in DP are i and j respectively.
5fill in blank
hardFill all three blanks to complete the function signature and return statement.
DSA C++
int [1](const std::string& word1, const std::string& word2) { int m = word1.size(), n = word2.size(); std::vector<std::vector<int>> dp(m + 1, std::vector<int>(n + 1)); // ... DP logic ... return [2][[3]][n]; }
Attempts:
3 left
💡 Hint
Common Mistakes
Returning dp[n][m] instead of dp[m][n].
Using wrong function name or variable names.
✗ Incorrect
The function is named editDistance, returns dp[m][n] which holds the final answer.
