Challenge - 5 Problems
Longest Common Substring Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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"));Attempts:
2 left
💡 Hint
Check the longest sequence of matching characters in order.
✗ Incorrect
The longest common substring between "abcde" and "abfde" is "ab" which has length 2.
🧠 Conceptual
intermediate1:30remaining
Understanding the Longest Common Substring Concept
Which of the following best describes the longest common substring between two strings?
Attempts:
2 left
💡 Hint
Think about continuous matching characters.
✗ Incorrect
The longest common substring requires characters to be contiguous and in the same order in both strings.
❓ Predict Output
advanced1: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"));Attempts:
2 left
💡 Hint
If one string is empty, no common substring exists.
✗ Incorrect
Since one string is empty, there can be no common substring, so the length is 0.
🔧 Debug
advanced2: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"));Attempts:
2 left
💡 Hint
Check how the 2D array dp is initialized.
✗ Incorrect
The dp array is initialized incorrectly using fill with the same inner array reference, causing undefined errors when assigning dp[i][j].
🚀 Application
expert2: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?
Attempts:
2 left
💡 Hint
All characters match continuously.
✗ Incorrect
Since both strings are identical and consist only of 'a's, the entire string is the longest common substring.
