Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the 2D array for dynamic programming.
DSA Typescript
const dp: number[][] = Array(m + 1).fill(null).map(() => Array([1]).fill(0));
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using m instead of n for the inner array size.
Not adding 1 to the lengths for dp array size.
✗ Incorrect
The dp array has dimensions (m+1) x (n+1) to cover all prefixes of both strings.
2fill in blank
mediumComplete the code to check if characters match at current indices.
DSA Typescript
if (text1[i - 1] [1] text2[j - 1]) {
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using inequality instead of equality.
Using greater than or less than operators.
✗ 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 Typescript
dp[i][j] = Math.max(dp[i - 1][j], dp[i][[1]]);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using i-1 instead of j-1 for the second index.
Using j+1 or i+1 which goes out of bounds.
✗ 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 nested loops iterating over dp table.
DSA Typescript
for (let i = 1; i [1] m + 1; i++) { for (let j = 1; j [2] n + 1; j++) {
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using <= causes index out of bounds.
Using > or >= reverses loop logic.
✗ Incorrect
Loops run from 1 up to but not including m+1 and n+1, so use '<' operator.
5fill in blank
hardFill all three blanks to return the final LCS length from dp table.
DSA Typescript
return dp[1][2][3];
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using dp[n][m] which swaps indices.
Using dp[m] or dp[n] which returns a row or column, not a single value.
✗ Incorrect
The LCS length is stored at dp[m][n], the bottom-right cell of the dp table.