Challenge - 5 Problems
LCS Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of LCS length calculation
What is the output of the following TypeScript code that calculates the length of the Longest Common Subsequence (LCS) between two strings?
DSA Typescript
function lcsLength(a: string, b: string): number {
const m = a.length, n = b.length;
const dp: number[][] = Array(m + 1).fill(null).map(() => Array(n + 1).fill(0));
for (let i = 1; i <= m; i++) {
for (let j = 1; j <= n; j++) {
if (a[i - 1] === b[j - 1]) {
dp[i][j] = dp[i - 1][j - 1] + 1;
} else {
dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
}
}
}
return dp[m][n];
}
console.log(lcsLength("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 correctly computes and returns 4.
❓ Predict Output
intermediate2:00remaining
Output of LCS sequence reconstruction
What is the output of the following TypeScript code that reconstructs the Longest Common Subsequence (LCS) string between two inputs?
DSA Typescript
function lcsSequence(a: string, b: string): string {
const m = a.length, n = b.length;
const dp: number[][] = Array(m + 1).fill(null).map(() => Array(n + 1).fill(0));
for (let i = 1; i <= m; i++) {
for (let j = 1; j <= n; j++) {
if (a[i - 1] === b[j - 1]) {
dp[i][j] = dp[i - 1][j - 1] + 1;
} else {
dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
}
}
}
let i = m, j = n;
let lcs = '';
while (i > 0 && j > 0) {
if (a[i - 1] === b[j - 1]) {
lcs = a[i - 1] + lcs;
i--;
j--;
} else if (dp[i - 1][j] > dp[i][j - 1]) {
i--;
} else {
j--;
}
}
return lcs;
}
console.log(lcsSequence("ABCBDAB", "BDCABA"));Attempts:
2 left
💡 Hint
Follow the dp table backwards to build the LCS string.
✗ Incorrect
The longest common subsequence between "ABCBDAB" and "BDCABA" is "BCAB" which is correctly reconstructed by the code.
🔧 Debug
advanced2:00remaining
Identify the error in LCS length code
What error will the following TypeScript code produce when trying to calculate the LCS length?
DSA Typescript
function lcsLength(a: string, b: string): number {
const m = a.length, n = b.length;
const dp: number[][] = Array(m).fill(Array(n).fill(0));
for (let i = 1; i <= m; i++) {
for (let j = 1; j <= n; j++) {
if (a[i - 1] === b[j - 1]) {
dp[i][j] = dp[i - 1][j - 1] + 1;
} else {
dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
}
}
}
return dp[m - 1][n - 1];
}
console.log(lcsLength("ABC", "AC"));Attempts:
2 left
💡 Hint
Check how the dp array is initialized and accessed.
✗ Incorrect
The dp array is created with Array(m) which has indices 0 to m-1, but the loop goes to i <= m accessing dp[m] which is undefined, causing a TypeError on assignment. Additionally, using fill with the same inner array shares references across rows.
🧠 Conceptual
advanced1:00remaining
Time complexity of LCS dynamic programming
What is the time complexity of the classic dynamic programming solution to find the length of the Longest Common Subsequence of two strings of lengths m and n?
Attempts:
1 left
💡 Hint
Consider the nested loops over both strings.
✗ Incorrect
The DP solution fills a 2D table of size (m+1) by (n+1), with two nested loops, so O(m * n) time.
🚀 Application
expert3:00remaining
Longest Common Subsequence count of distinct subsequences
Given two strings, how many distinct longest common subsequences can they have? For example, for strings "ABCBDAB" and "BDCABA", how many distinct LCS strings exist?
Attempts:
2 left
💡 Hint
There can be multiple LCS strings of the same maximum length.
✗ Incorrect
For the example strings, there are 3 distinct LCS strings of length 4: "BCAB", "BDAB", and "BCBA".