Challenge - 5 Problems
LCS Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of LCS length calculation
What is the output of the following C++ code that calculates the length of the Longest Common Subsequence (LCS) between two strings?
DSA C++
int lcsLength(const string &X, const string &Y) { int m = X.size(), n = Y.size(); vector<vector<int>> dp(m + 1, vector<int>(n + 1, 0)); for (int i = 1; i <= m; i++) { for (int j = 1; j <= n; j++) { if (X[i - 1] == Y[j - 1]) dp[i][j] = dp[i - 1][j - 1] + 1; else dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); } } return dp[m][n]; } int main() { string X = "AGGTAB"; string Y = "GXTXAYB"; cout << lcsLength(X, Y) << endl; return 0; }
Attempts:
2 left
💡 Hint
Trace the matching characters between the two strings and count the longest sequence.
✗ Incorrect
The longest common subsequence between "AGGTAB" and "GXTXAYB" is "GTAB", which has length 4. The code correctly computes and prints 4.
❓ Predict Output
intermediateOutput of LCS sequence reconstruction
What is the output of the following C++ code that reconstructs and prints the Longest Common Subsequence (LCS) between two strings?
DSA C++
string lcsSequence(const string &X, const string &Y) {
int m = X.size(), n = Y.size();
vector<vector<int>> dp(m + 1, vector<int>(n + 1, 0));
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
if (X[i - 1] == Y[j - 1])
dp[i][j] = dp[i - 1][j - 1] + 1;
else
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
}
}
int i = m, j = n;
string lcs = "";
while (i > 0 && j > 0) {
if (X[i - 1] == Y[j - 1]) {
lcs = X[i - 1] + lcs;
i--;
j--;
} else if (dp[i - 1][j] > dp[i][j - 1]) {
i--;
} else {
j--;
}
}
return lcs;
}
int main() {
string X = "ABCBDAB";
string Y = "BDCABA";
cout << lcsSequence(X, Y) << endl;
return 0;
}Attempts:
2 left
💡 Hint
Trace back from dp[m][n] to find the common characters in order.
✗ Incorrect
The longest common subsequence between "ABCBDAB" and "BDCABA" is "BDAB". The code reconstructs this sequence correctly.
🧠 Conceptual
advancedTime complexity of LCS dynamic programming solution
What is the time complexity of the classic dynamic programming solution to find the length of the Longest Common Subsequence (LCS) of two strings of lengths m and n?
Attempts:
2 left
💡 Hint
Consider the size of the DP table and how many cells are filled.
✗ Incorrect
The DP table has size (m+1)*(n+1), and each cell is computed in constant time, so total time is O(m*n).
🔧 Debug
advancedIdentify the error in LCS length calculation code
What error will the following C++ code produce when trying to calculate the LCS length?
DSA C++
int lcsLength(const string &X, const string &Y) { int m = X.size(), n = Y.size(); vector<vector<int>> dp(m, vector<int>(n, 0)); for (int i = 1; i <= m; i++) { for (int j = 1; j <= n; j++) { if (X[i - 1] == Y[j - 1]) dp[i][j] = dp[i - 1][j - 1] + 1; else dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); } } return dp[m - 1][n - 1]; }
Attempts:
2 left
💡 Hint
Check the size of dp and the indices used to access it.
✗ Incorrect
The dp vector is sized m x n but the loops access dp[i][j] for i and j up to m and n inclusive, causing out of range access.
🚀 Application
expertNumber of distinct LCS sequences
Given two strings X and Y, which approach correctly counts the number of distinct Longest Common Subsequences (LCS) between them?
Attempts:
2 left
💡 Hint
Think about how to track both length and count simultaneously in DP.
✗ Incorrect
The correct method uses two DP tables: one for LCS length and one for counting sequences, updating counts carefully when lengths tie or increase.
