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][s2.length() + 1];
Attempts:
3 left
💡 Hint
Common Mistakes
Using s2.length() instead of s1.length() for rows.
Not adding 1 to the length.
✗ Incorrect
We create a 2D array with rows equal to the length of the first string plus one.
2fill in blank
mediumComplete the code to check if characters at positions i-1 and j-1 match.
DSA Java
if (s1.charAt(i - 1) [1] s2.charAt(j - 1)) {
Attempts:
3 left
💡 Hint
Common Mistakes
Using '!=' which checks for inequality.
Using comparison operators like '>' or '<' which are incorrect here.
✗ Incorrect
We check if characters are equal to update the dp table.
3fill in blank
hardFix the error in updating the dp table when characters match.
DSA Java
dp[i][j] = dp[i - 1][j - 1] [1] 1;
Attempts:
3 left
💡 Hint
Common Mistakes
Using '-' which decreases length incorrectly.
Using '*' or '/' which are not suitable here.
✗ Incorrect
We add 1 to the previous diagonal value to extend the substring length.
4fill in blank
hardFill both blanks to update maxLength and endIndex correctly.
DSA Java
if (dp[i][j] [1] maxLength) { maxLength = dp[i][j]; endIndex = i [2] 1; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' in comparison.
Adding 1 to i instead of subtracting 1.
✗ Incorrect
We update maxLength if current dp value is greater, and set endIndex to i - 1.
5fill in blank
hardFill all three blanks to extract the longest common substring from s1.
DSA Java
String result = s1.substring(endIndex - [1] + [2], endIndex + [3]);
Attempts:
3 left
💡 Hint
Common Mistakes
Using endIndex directly as the start index.
Forgetting the +1 adjustment for the exclusive end index.
Using 0 instead of 1 in the calculations.
✗ Incorrect
The longest common substring starts at endIndex - maxLength + 1 and goes up to but not including endIndex + 1.
