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 Java code that calculates the length of the longest common substring between two strings?
DSA Java
public class Main { public static int longestCommonSubstring(String s1, String s2) { int maxLen = 0; int[][] dp = new int[s1.length() + 1][s2.length() + 1]; for (int i = 1; i <= s1.length(); i++) { for (int j = 1; j <= s2.length(); j++) { if (s1.charAt(i - 1) == s2.charAt(j - 1)) { dp[i][j] = dp[i - 1][j - 1] + 1; maxLen = Math.max(maxLen, dp[i][j]); } else { dp[i][j] = 0; } } } return maxLen; } public static void main(String[] args) { System.out.println(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.
❓ Predict Output
intermediateLongest Common Substring Result with Overlapping Substrings
What will be printed by this Java program that finds the longest common substring length?
DSA Java
public class Main { public static int longestCommonSubstring(String s1, String s2) { int maxLen = 0; int[][] dp = new int[s1.length() + 1][s2.length() + 1]; for (int i = 1; i <= s1.length(); i++) { for (int j = 1; j <= s2.length(); j++) { if (s1.charAt(i - 1) == s2.charAt(j - 1)) { dp[i][j] = dp[i - 1][j - 1] + 1; maxLen = Math.max(maxLen, dp[i][j]); } else { dp[i][j] = 0; } } } return maxLen; } public static void main(String[] args) { System.out.println(longestCommonSubstring("ababc", "babca")); } }
Attempts:
2 left
💡 Hint
Look for the longest continuous matching substring.
✗ Incorrect
The longest common substring is "abca" starting at index 1 in s1 and index 1 in s2, length 4.
🔧 Debug
advancedIdentify the Error in Longest Common Substring Implementation
What error will this Java code produce when run?
DSA Java
public class Main { public static int longestCommonSubstring(String s1, String s2) { int maxLen = 0; int[][] dp = new int[s1.length()][s2.length()]; for (int i = 1; i <= s1.length(); i++) { for (int j = 1; j <= s2.length(); j++) { if (s1.charAt(i - 1) == s2.charAt(j - 1)) { dp[i][j] = dp[i - 1][j - 1] + 1; maxLen = Math.max(maxLen, dp[i][j]); } else { dp[i][j] = 0; } } } return maxLen; } public static void main(String[] args) { System.out.println(longestCommonSubstring("abc", "abc")); } }
Attempts:
2 left
💡 Hint
Check array size and loop indices carefully.
✗ Incorrect
The dp array is sized s1.length() x s2.length(), but loops start at 1 and access dp[i][j], causing index out of bounds.
🧠 Conceptual
advancedLongest Common Substring vs Longest Common Subsequence
Which statement correctly describes the difference between Longest Common Substring and Longest Common Subsequence?
Attempts:
2 left
💡 Hint
Think about whether characters must be next to each other.
✗ Incorrect
Longest Common Substring needs characters to be next to each other in both strings. Longest Common Subsequence allows skipping characters.
🚀 Application
expertLongest Common Substring Length for Large Inputs
Given two strings each of length 100000 containing only 'a' characters, what is the length of their longest common substring?
Attempts:
2 left
💡 Hint
All characters match and are contiguous.
✗ Incorrect
Since both strings are identical and contain only 'a's, the entire string is the longest common substring.
