Bird
0
0
DSA Typescriptprogramming~20 mins

Longest Common Substring in DSA Typescript - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Longest Common Substring Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Longest Common Substring Length Calculation
What is the output of the following TypeScript code that calculates the length of the longest common substring between two strings?
DSA Typescript
function longestCommonSubstring(s1: string, s2: string): number {
  const m = s1.length;
  const n = s2.length;
  let maxLen = 0;
  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;
        if (dp[i][j] > maxLen) {
          maxLen = dp[i][j];
        }
      }
    }
  }
  return maxLen;
}

console.log(longestCommonSubstring("abcde", "abfde"));
A4
B3
C2
D1
Attempts:
2 left
💡 Hint
Check the longest sequence of matching characters in order.
🧠 Conceptual
intermediate
1:30remaining
Understanding the Longest Common Substring Concept
Which of the following best describes the longest common substring between two strings?
AThe longest sequence of characters that appear in both strings in the same order and contiguously.
BThe longest sequence of characters that appear in both strings in any order, not necessarily contiguous.
CThe longest sequence of characters that appear in both strings but can be separated by other characters.
DThe longest sequence of characters that appear only in the first string.
Attempts:
2 left
💡 Hint
Think about continuous matching characters.
Predict Output
advanced
1:30remaining
Output of Longest Common Substring with Empty String
What is the output of this TypeScript code when one input string is empty?
DSA Typescript
function longestCommonSubstring(s1: string, s2: string): number {
  const m = s1.length;
  const n = s2.length;
  let maxLen = 0;
  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;
        if (dp[i][j] > maxLen) {
          maxLen = dp[i][j];
        }
      }
    }
  }
  return maxLen;
}

console.log(longestCommonSubstring("", "abc"));
A3
B0
C1
Dundefined
Attempts:
2 left
💡 Hint
If one string is empty, no common substring exists.
🔧 Debug
advanced
2:00remaining
Identify the Error in Longest Common Substring Code
What error will this TypeScript code produce when run?
DSA Typescript
function longestCommonSubstring(s1: string, s2: string): number {
  const m = s1.length;
  const n = s2.length;
  let maxLen = 0;
  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;
        if (dp[i][j] > maxLen) {
          maxLen = dp[i][j];
        }
      }
    }
  }
  return maxLen;
}

console.log(longestCommonSubstring("abc", "abc"));
ASyntaxError: Unexpected token '['
BReferenceError: dp is not defined
COutput: 3
DTypeError: Cannot set property '1' of undefined
Attempts:
2 left
💡 Hint
Check how the 2D array dp is initialized.
🚀 Application
expert
2:30remaining
Longest Common Substring Length for Large Inputs
Given two strings each of length 1000 consisting of only 'a' characters, what is the length of their longest common substring?
A1000
B1
C0
D999
Attempts:
2 left
💡 Hint
All characters match continuously.