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 Go code that calculates the length of the longest common substring between two strings?
DSA Go
package main import "fmt" func longestCommonSubstring(s1, s2 string) int { m, n := len(s1), len(s2) dp := make([][]int, m+1) for i := range dp { dp[i] = make([]int, n+1) } maxLen := 0 for i := 1; i <= m; i++ { for 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 } func main() { fmt.Println(longestCommonSubstring("abcde", "abfce")) }
Attempts:
2 left
💡 Hint
Check the longest sequence of matching characters in order.
✗ Incorrect
The longest common substring between "abcde" and "abfce" is "ab" which has length 2.
🧠 Conceptual
intermediateUnderstanding Longest Common Substring vs Longest Common Subsequence
Which statement correctly describes the difference between the longest common substring and the longest common subsequence?
Attempts:
2 left
💡 Hint
Think about whether characters must be next to each other.
✗ Incorrect
A substring is a continuous part of a string, while a subsequence can skip characters.
🔧 Debug
advancedIdentify the Error in Longest Common Substring Implementation
What error does the following Go code produce when run, and why?
package main
import "fmt"
func longestCommonSubstring(s1, s2 string) int {
m, n := len(s1), len(s2)
dp := make([][]int, m)
for i := range dp {
dp[i] = make([]int, n)
}
maxLen := 0
for i := 1; i <= m; i++ {
for 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
}
func main() {
fmt.Println(longestCommonSubstring("abc", "abc"))
}
Attempts:
2 left
💡 Hint
Check how dp array is created and accessed.
✗ Incorrect
The dp array is created with size m x n but the loops access dp[i][j] where i and j go up to m and n inclusive, causing out of range error.
🚀 Application
advancedLongest Common Substring in DNA Sequences
Given two DNA sequences "AGCTTAGC" and "CTTAGGCA", what is the longest common substring?
Attempts:
2 left
💡 Hint
Look for the longest continuous matching segment.
✗ Incorrect
"CTTAG" appears continuously in both sequences and is the longest common substring.
❓ Predict Output
expertOutput of Optimized Longest Common Substring Length Calculation
What is the output of this optimized Go code that calculates the length of the longest common substring?
DSA Go
package main import "fmt" func longestCommonSubstring(s1, s2 string) int { m, n := len(s1), len(s2) prev := make([]int, n+1) maxLen := 0 for i := 1; i <= m; i++ { curr := make([]int, n+1) for j := 1; j <= n; j++ { if s1[i-1] == s2[j-1] { curr[j] = prev[j-1] + 1 if curr[j] > maxLen { maxLen = curr[j] } } } prev = curr } return maxLen } func main() { fmt.Println(longestCommonSubstring("banana", "ananas")) }
Attempts:
2 left
💡 Hint
Check the longest continuous matching substring between the two strings.
✗ Incorrect
The longest common substring between "banana" and "ananas" is "anan" with length 4.
