Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the 2D array for dynamic programming.
DSA Javascript
const dp = Array(m + 1).fill(null).map(() => Array([1]).fill(0));
Attempts:
3 left
💡 Hint
Common Mistakes
Using m instead of n for the second dimension.
Not adding 1 to the lengths.
✗ Incorrect
We create a 2D array with dimensions (m+1) x (n+1) to store LCS lengths.
2fill in blank
mediumComplete the condition to check if characters match in the LCS algorithm.
DSA Javascript
if (text1[i - 1] [1] text2[j - 1]) {
Attempts:
3 left
💡 Hint
Common Mistakes
Using != instead of ===.
Using < or > which are not correct for equality check.
✗ 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 updating dp when characters do not match.
DSA Javascript
dp[i][j] = Math.max(dp[i - 1][j], dp[i][[1]]);
Attempts:
3 left
💡 Hint
Common Mistakes
Using i - 1 instead of j - 1.
Using j + 1 or i + 1 which go out of bounds.
✗ Incorrect
When characters differ, take max of dp[i-1][j] and dp[i][j-1].
4fill in blank
hardFill both blanks to complete the nested loops iterating over strings.
DSA Javascript
for (let i = 1; i [1] m + 1; i++) { for (let j = 1; j [2] n + 1; j++) {
Attempts:
3 left
💡 Hint
Common Mistakes
Using <= which causes index out of range.
Using > or >= which are incorrect for loop conditions.
✗ Incorrect
Loops run from 1 up to but not including m+1 and n+1 using < operator.
5fill in blank
hardFill all three blanks to return the final LCS length from dp array.
DSA Javascript
return dp[[1]][[2]]; // length of LCS for text1 and text2 // Variables: m = [3]
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up m and n indices.
Using incorrect variables for lengths.
✗ Incorrect
Return dp[m][n] where m and n are lengths of text1 and text2 respectively.
