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 Python code that calculates the length of the longest common substring between two strings?
DSA Python
def longest_common_substring(s1, s2): m, n = len(s1), len(s2) dp = [[0] * (n + 1) for _ in range(m + 1)] max_len = 0 for i in range(1, m + 1): for j in range(1, n + 1): if s1[i - 1] == s2[j - 1]: dp[i][j] = dp[i - 1][j - 1] + 1 max_len = max(max_len, dp[i][j]) return max_len print(longest_common_substring("abcdef", "zcdemf"))
Attempts:
2 left
💡 Hint
Look for the longest sequence of matching characters in order.
✗ Incorrect
The longest common substring between "abcdef" and "zcdemf" is "cde" which has length 3.
❓ Predict Output
intermediateLongest Common Substring Result String
What is the output of this code that returns the longest common substring itself (not just length)?
DSA Python
def longest_common_substring_str(s1, s2): m, n = len(s1), len(s2) dp = [[0] * (n + 1) for _ in range(m + 1)] max_len = 0 end_index = 0 for i in range(1, m + 1): for j in range(1, n + 1): if s1[i - 1] == s2[j - 1]: dp[i][j] = dp[i - 1][j - 1] + 1 if dp[i][j] > max_len: max_len = dp[i][j] end_index = i return s1[end_index - max_len:end_index] print(longest_common_substring_str("banana", "ananas"))
Attempts:
2 left
💡 Hint
Check which substring appears continuously in both strings.
✗ Incorrect
The longest common substring between "banana" and "ananas" is "anana" which has length 5.
🧠 Conceptual
advancedUnderstanding Dynamic Programming Table for Longest Common Substring
In the dynamic programming table used for longest common substring, what does the value dp[i][j] represent?
Attempts:
2 left
💡 Hint
Focus on the substring ending exactly at those positions.
✗ Incorrect
dp[i][j] stores the length of the longest common substring that ends exactly at s1[i-1] and s2[j-1]. It resets to zero if characters don't match.
🔧 Debug
advancedIdentify the Error in Longest Common Substring Code
What error will this code produce when run?
DSA Python
def lcs_wrong(s1, s2): m, n = len(s1), len(s2) dp = [[0] * n for _ in range(m)] max_len = 0 for i in range(1, m + 1): for j in range(1, n + 1): if s1[i - 1] == s2[j - 1]: dp[i][j] = dp[i - 1][j - 1] + 1 max_len = max(max_len, dp[i][j]) return max_len print(lcs_wrong("abc", "abc"))
Attempts:
2 left
💡 Hint
Check how dp is initialized and accessed.
✗ Incorrect
dp is initialized with size m x n but accessed with indices up to m and n inclusive, causing IndexError.
🚀 Application
expertLongest Common Substring in Multiple Strings
Given three strings, which approach correctly finds the longest common substring among all three?
Attempts:
2 left
💡 Hint
Consider how to track substring matches across three strings at once.
✗ Incorrect
To find longest common substring among three strings, a 3D DP table is needed to track substring matches ending at each position in all three strings simultaneously.
