0
0
DSA Typescriptprogramming~20 mins

Longest Common Subsequence in DSA Typescript - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
LCS Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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"));
A5
B3
C6
D4
Attempts:
2 left
💡 Hint
Trace the matching characters and count the longest sequence common to both strings.
Predict Output
intermediate
2: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"));
A"BCAB"
B"BDAB"
C"BDCB"
D"BCBA"
Attempts:
2 left
💡 Hint
Follow the dp table backwards to build the LCS string.
🔧 Debug
advanced
2: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"));
ANo error, returns correct length 2
BIndexError: list index out of range
CTypeError: Cannot set property '1' of undefined
DReturns incorrect LCS length 1
Attempts:
2 left
💡 Hint
Check how the dp array is initialized and accessed.
🧠 Conceptual
advanced
1: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?
AO(m + n)
BO(m * n)
CO(m^2 + n^2)
DO(2^(m + n))
Attempts:
1 left
💡 Hint
Consider the nested loops over both strings.
🚀 Application
expert
3: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?
A3
B2
C1
D4
Attempts:
2 left
💡 Hint
There can be multiple LCS strings of the same maximum length.