Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the DP table with zeros.
DSA C++
int dp[m+1][n+1]; for (int i = 0; i <= m; i++) { for (int j = 0; j <= n; j++) { dp[i][j] = [1]; } }
Attempts:
3 left
💡 Hint
Common Mistakes
Initializing with 1 instead of 0
Using negative values which are incorrect for LCS length
Not initializing the DP table at all
✗ Incorrect
The DP table for LCS is initialized with zeros to represent empty subsequences.
2fill in blank
mediumComplete the code to check if characters match in the two strings.
DSA C++
if (X[i-1] [1] Y[j-1]) { dp[i][j] = dp[i-1][j-1] + 1; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using inequality instead of equality
Using greater than or less than operators which are invalid for char comparison here
✗ Incorrect
We check if characters at positions i-1 and j-1 are equal to extend the subsequence.
3fill in blank
hardFix the error in the else block to correctly update dp[i][j].
DSA C++
else { dp[i][j] = std::max(dp[i-1][j], [1]); }
Attempts:
3 left
💡 Hint
Common Mistakes
Using indices out of bounds like j+1 or i+1
Using dp[i-1][j-1] which is for matching case
✗ Incorrect
When characters don't match, dp[i][j] is max of dp[i-1][j] and dp[i][j-1].
4fill in blank
hardFill both blanks to complete the loop headers for filling the DP table.
DSA C++
for (int [1] = 1; [1] <= m; [1]++) { for (int [2] = 1; [2] <= n; [2]++) { // fill dp[i][j] } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using same variable for both loops
Using uncommon variable names causing confusion
✗ Incorrect
The outer loop uses i for string X, inner loop uses j for string Y.
5fill in blank
hardFill all three blanks to complete the code that returns the LCS length.
DSA C++
int lcsLength = dp[[1]][[2]]; return [3];
Attempts:
3 left
💡 Hint
Common Mistakes
Returning dp[0][0] which is always zero
Returning m or n instead of the computed length
✗ Incorrect
The LCS length is stored at dp[m][n], which we return.
