Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the maximum length variable for the longest common substring.
DSA C++
int maxLength = [1];Attempts:
3 left
💡 Hint
Common Mistakes
Initializing maxLength to 1 or a negative number.
✗ Incorrect
We start with maxLength as 0 because initially, we haven't found any common substring.
2fill in blank
mediumComplete the code to create a 2D vector dp with dimensions (m+1) x (n+1) initialized to zero.
DSA C++
std::vector<std::vector<int>> dp([1], std::vector<int>([2], 0));
Attempts:
3 left
💡 Hint
Common Mistakes
Using m and n instead of m+1 and n+1, causing index errors.
✗ Incorrect
We use m+1 and n+1 to include the base case row and column for empty substrings.
3fill in blank
hardFix the error in the condition to check if characters at positions i-1 and j-1 in strings s1 and s2 match.
DSA C++
if (s1[i-1] [1] s2[j-1]) {
Attempts:
3 left
💡 Hint
Common Mistakes
Using '!=' instead of '==' causing logic to fail.
✗ Incorrect
We check if characters are equal to extend the common substring.
4fill in blank
hardFill both blanks to update dp[i][j] and maxLength when characters match.
DSA C++
dp[i][j] = dp[i-1][j-1] [1] 1; if (dp[i][j] [2] maxLength) maxLength = dp[i][j];
Attempts:
3 left
💡 Hint
Common Mistakes
Using '-' instead of '+' causing wrong substring length.
Using '<' instead of '>' in comparison.
✗ Incorrect
We add 1 to the previous diagonal dp value to extend substring length and update maxLength if current is greater.
5fill in blank
hardFill all three blanks to complete the nested loops iterating over strings s1 and s2.
DSA C++
for (int i = 1; i [1] m+1; i[2]) { for (int j = 1; j [3] n+1; j[2]) {
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<=' causing out-of-bound errors.
Using '--' causing infinite loops.
✗ Incorrect
Loops run from 1 to m and n (exclusive of m+1 and n+1) with increment ++ to cover all characters.
