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([1]).fill(0).map(() => Array(m + 1).fill(0));
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using m+1 instead of n+1 for rows.
Not adding 1 to the length causing index errors.
✗ Incorrect
We create a 2D array with (n+1) rows to cover all prefixes of the first string including empty prefix.
2fill in blank
mediumComplete the code to update the dp table when characters match.
DSA Typescript
if (s1[i - 1] === s2[j - 1]) { dp[i][j] = dp[i - 1][[1]] + 1; } else { dp[i][j] = 0; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using dp[i-1][j] or dp[i][j-1] instead of dp[i-1][j-1].
Adding 1 to the wrong index.
✗ Incorrect
When characters match, we extend the substring by 1 from the previous diagonal cell dp[i-1][j-1].
3fill in blank
hardFix the error in updating the maximum length of the longest common substring.
DSA Typescript
if (dp[i][j] > [1]) { maxLen = dp[i][j]; endIndex = i; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing with endIndex instead of maxLen.
Using i or j as comparison variable.
✗ Incorrect
We compare dp[i][j] with maxLen to update the maximum length found so far.
4fill in blank
hardFill both blanks to extract the longest common substring from s1.
DSA Typescript
const longestSubstring = s1.slice([1], [2]);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using maxLen as start index.
Using 0 as start index instead of calculated start.
✗ Incorrect
We slice s1 from (endIndex - maxLen) to endIndex to get the longest common substring.
5fill in blank
hardFill all three blanks to complete the nested loops for dp table computation.
DSA Typescript
for (let i = 1; i <= [1]; i++) { for (let j = 1; j <= [2]; j++) { if (s1[i - 1] === s2[j - 1]) { dp[i][j] = dp[i - 1][j - 1] + [3]; } else { dp[i][j] = 0; } } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 as increment instead of 1.
Swapping n and m in loops.
✗ Incorrect
The outer loop runs from 1 to n, inner loop from 1 to m, and we add 1 when characters match.