Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to return 0 when either string is empty.
DSA Java
if (m == 0 || n == 0) return [1];
Attempts:
3 left
💡 Hint
Common Mistakes
Returning 1 instead of 0
Returning negative values
Returning sum of lengths
✗ Incorrect
If either string length is zero, the longest common subsequence length is 0.
2fill in blank
mediumComplete the code to check if last characters of both strings match.
DSA Java
if (X.charAt(m - 1) == Y.charAt(n - 1)) { return 1 + lcs(X, Y, m - 1, n - 1); } else { return Math.max(lcs(X, Y, m - 1, n), [1]); }
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong indices in recursive call
Using m + 1 which is out of bounds
Repeating the same call twice
✗ Incorrect
If last characters don't match, we take max of excluding last char from either string.
3fill in blank
hardFix the error in the memoization check to avoid recomputation.
DSA Java
if (dp[m][n] != [1]) { return dp[m][n]; }
Attempts:
3 left
💡 Hint
Common Mistakes
Checking for 0 which can be a valid LCS length
Checking for null in int array
Using Integer.MAX_VALUE which is unrelated
✗ Incorrect
We initialize dp with -1 to mark uncomputed states, so check for -1 to detect computed values.
4fill in blank
hardFill both blanks to initialize the dp array with -1 for all entries.
DSA Java
for (int i = 0; i <= m; i++) { for (int j = 0; j <= n; j++) { dp[[1]][[2]] = -1; } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using fixed indices like m or n instead of loop variables
Mixing i and j positions
✗ Incorrect
We use i and j as indices to fill dp[i][j] with -1 for all valid i and j.
5fill in blank
hardFill all three blanks to store and return the computed LCS length in dp.
DSA Java
if (X.charAt(m - 1) == Y.charAt(n - 1)) { dp[m][n] = 1 + [1]; } else { dp[m][n] = Math.max([2], [3]); } return dp[m][n];
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong recursive calls
Not storing results in dp
Returning without storing
✗ Incorrect
We store the recursive results in dp to avoid recomputation: 1 + lcs for matching chars, else max of excluding last char from either string.
