Challenge - 5 Problems
Longest Common Substring Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Longest Common Substring Length Calculation
What is the output of the following JavaScript code that calculates the length of the longest common substring between two strings?
DSA Javascript
function longestCommonSubstring(s1, s2) {
const m = s1.length, n = s2.length;
let maxLen = 0;
const dp = 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("abcdef", "zcdemf"));Attempts:
2 left
💡 Hint
Look for the longest sequence of characters that appear consecutively in both strings.
✗ Incorrect
The longest common substring between "abcdef" and "zcdemf" is "cde" which has length 3.
🧠 Conceptual
intermediateUnderstanding the Longest Common Substring Concept
Which of the following best describes the longest common substring between two strings?
Attempts:
2 left
💡 Hint
Focus on the word 'substring' which means consecutive characters.
✗ Incorrect
A substring requires characters to be consecutive and in the same order in both strings.
❓ Predict Output
advancedOutput of Longest Common Substring with Empty String
What is the output of this JavaScript code when one input string is empty?
DSA Javascript
function longestCommonSubstring(s1, s2) {
const m = s1.length, n = s2.length;
let maxLen = 0;
const dp = 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
advancedIdentify the Error in Longest Common Substring Code
What error will this code produce when run?
DSA Javascript
function longestCommonSubstring(s1, s2) {
const m = s1.length, n = s2.length;
let maxLen = 0;
const dp = Array(m).fill(null).map(() => 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 created and accessed.
✗ Incorrect
Using Array(m).fill(Array(n).fill(0)) creates multiple references to the same inner array, causing dp[i] to be undefined for i >= m.
🚀 Application
expertLongest 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 are the same in both strings.
✗ Incorrect
Since both strings are identical and consist only of 'a's, the entire string is the longest common substring.
