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 Python code that calculates the length of the Longest Common Subsequence (LCS) between two strings?
DSA Python
def lcs_length(X, Y): m = len(X) n = len(Y) dp = [[0] * (n + 1) for _ in range(m + 1)] for i in range(1, m + 1): for j in range(1, n + 1): 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] print(lcs_length("AGGTAB", "GXTXAYB"))
Attempts:
2 left
💡 Hint
Trace the matching characters and count the longest sequence common to both strings.
✗ Incorrect
The longest common subsequence between "AGGTAB" and "GXTXAYB" is "GTAB" which has length 4. The code returns 4.
❓ Predict Output
intermediateOutput of LCS sequence reconstruction
What is the output of the following Python code that reconstructs the Longest Common Subsequence (LCS) string between two strings?
DSA Python
def lcs_sequence(X, Y): m = len(X) n = len(Y) dp = [[0] * (n + 1) for _ in range(m + 1)] for i in range(1, m + 1): for j in range(1, n + 1): 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]) i, j = m, n lcs_str = [] while i > 0 and j > 0: if X[i - 1] == Y[j - 1]: lcs_str.append(X[i - 1]) i -= 1 j -= 1 elif dp[i - 1][j] > dp[i][j - 1]: i -= 1 else: j -= 1 return ''.join(reversed(lcs_str)) print(lcs_sequence("ABCBDAB", "BDCABA"))
Attempts:
2 left
💡 Hint
Follow the path from dp[m][n] back to dp[0][0] to build the LCS string.
✗ Incorrect
The longest common subsequence between "ABCBDAB" and "BDCABA" is "BDAB".
🧠 Conceptual
advancedUnderstanding LCS dynamic programming table size
Given two strings of lengths m and n, what is the size of the dynamic programming table used to compute the Longest Common Subsequence (LCS)?
Attempts:
2 left
💡 Hint
The table includes an extra row and column for base cases.
✗ Incorrect
The DP table has (m+1) rows and (n+1) columns to handle empty prefixes of the strings.
🔧 Debug
advancedIdentify the error in LCS length code
What error will the following code produce when trying to compute the LCS length?
DSA Python
def lcs_length(X, Y): m = len(X) n = len(Y) dp = [[0] * n for _ in range(m)] for i in range(m): for j in range(n): if X[i] == Y[j]: 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] print(lcs_length("ABC", "AC"))
Attempts:
2 left
💡 Hint
Check how dp[i-1][j-1] is accessed when i or j is 0.
✗ Incorrect
The code does not handle the base case properly and tries to access dp[-1][-1] which causes IndexError.
🚀 Application
expertNumber of distinct LCS sequences
Given two strings "ABCBDAB" and "BDCABA", how many distinct Longest Common Subsequences (LCS) of maximum length exist?
Attempts:
2 left
💡 Hint
There can be multiple LCS strings of the same maximum length; count them carefully.
✗ Incorrect
For these strings, there are 3 distinct LCS sequences of length 4: "BCAB", "BDAB", and "BCBA".
