Bird
Raised Fist0
DSA Typescriptprogramming~20 mins

Longest Common Subsequence in DSA Typescript - Practice Problems & Challenges

Choose your learning style10 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.

Practice

(1/5)
1. What does the Longest Common Subsequence (LCS) between two strings represent?
easy
A. The total number of characters in both strings combined
B. The longest sequence of characters that appear in both strings in the same order, but not necessarily consecutively
C. The shortest sequence of characters to convert one string into another
D. The longest substring that appears in both strings consecutively

Solution

  1. Step 1: Understand the definition of LCS

    The LCS is the longest sequence that appears in both strings in the same order but not necessarily next to each other.
  2. Step 2: Differentiate from substring and edit distance

    Unlike substring, LCS does not require characters to be consecutive. It is not about shortest edits either.
  3. Final Answer:

    The longest sequence of characters that appear in both strings in the same order, but not necessarily consecutively -> Option B
  4. Quick Check:

    LCS means longest common sequence, not substring or edits [OK]
Hint: LCS is about order, not consecutive characters [OK]
Common Mistakes:
  • Confusing LCS with longest common substring
  • Thinking LCS requires consecutive characters
  • Mixing LCS with edit distance or shortest transformation
2. Which of the following TypeScript function signatures correctly represents a function to compute LCS length between two strings s1 and s2?
easy
A. function lcsLength(s1: string, s2: string): string[]
B. function lcsLength(s1: number[], s2: number[]): string
C. function lcsLength(s1: string, s2: string): void
D. function lcsLength(s1: string, s2: string): number

Solution

  1. Step 1: Identify input and output types for LCS length

    The function takes two strings and returns a number representing the length of their LCS.
  2. Step 2: Check each option's signature

    function lcsLength(s1: string, s2: string): number matches input strings and returns a number. Others either use wrong input types or return wrong types.
  3. Final Answer:

    function lcsLength(s1: string, s2: string): number -> Option D
  4. Quick Check:

    Input strings, output number for LCS length [OK]
Hint: LCS length function returns a number, inputs are strings [OK]
Common Mistakes:
  • Using arrays instead of strings as input
  • Returning string or void instead of number
  • Confusing LCS length with LCS sequence output
3. What is the output of the following TypeScript code snippet?
function lcsLength(s1: string, s2: string): number {
  const m = s1.length, n = s2.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 (s1[i - 1] === s2[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'))
medium
A. 4
B. 5
C. 3
D. 6

Solution

  1. Step 1: Understand the LCS dynamic programming table

    The code builds a 2D table dp where dp[i][j] stores LCS length of s1[0..i-1] and s2[0..j-1].
  2. Step 2: Trace LCS for 'AGGTAB' and 'GXTXAYB'

    The LCS is 'GTAB' with length 4, but careful tracing shows length 4 is correct. However, the known LCS length for these strings is 4.
  3. Final Answer:

    4 -> Option A
  4. Quick Check:

    LCS('AGGTAB','GXTXAYB') = 4 [OK]
Hint: Trace dp table or recall classic example LCS length 4 [OK]
Common Mistakes:
  • Confusing LCS length with substring length
  • Miscounting matching characters
  • Off-by-one errors in indexing
4. Identify the bug in the following TypeScript code that attempts to compute the LCS length:
function lcsLength(s1: string, s2: string): number {
  const m = s1.length, n = s2.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 (s1[i - 1] === s2[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];
}
medium
A. dp array initialization shares inner arrays causing incorrect updates
B. Loop indices should start from 0 instead of 1
C. Return statement should be dp[m][n] instead of dp[m - 1][n - 1]
D. Comparison should be s1[i] === s2[j] instead of s1[i - 1] === s2[j - 1]

Solution

  1. Step 1: Analyze dp array initialization

    Using Array(m).fill(Array(n).fill(0)) creates m references to the same inner array, causing shared rows.
  2. Step 2: Understand impact on dp updates

    Updating dp[i][j] affects all rows because they point to the same array, corrupting the dp table.
  3. Final Answer:

    dp array initialization shares inner arrays causing incorrect updates -> Option A
  4. Quick Check:

    Array fill with same inner array causes shared references [OK]
Hint: Use map to create independent inner arrays for dp [OK]
Common Mistakes:
  • Using fill with array causes shared references
  • Incorrect dp return indices
  • Off-by-one errors in loops
5. Given two strings s1 = 'ABCBDAB' and s2 = 'BDCABA', which of the following is a valid Longest Common Subsequence?
hard
A. 'BDCA'
B. 'BCBD'
C. 'BDAB'
D. 'BDA'

Solution

  1. Step 1: Determine length of LCS

    The LCS length for these strings is 4.
  2. Step 2: Check each option for validity and length

    'BDAB' is length 4 and appears in order in both strings. Others are either shorter or invalid sequences.
  3. Final Answer:

    'BDAB' -> Option C
  4. Quick Check:

    Valid LCS must be subsequence and max length 4 [OK]
Hint: Check subsequence order and length equals LCS length [OK]
Common Mistakes:
  • Choosing subsequences not in order in both strings
  • Picking shorter subsequences ignoring max length
  • Confusing substring with subsequence
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard