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 C code that calculates the length of the longest common substring between two strings?
DSA C
int longestCommonSubstring(char *s1, char *s2) { int maxLen = 0; int len1 = strlen(s1); int len2 = strlen(s2); int dp[len1+1][len2+1]; for (int i = 0; i <= len1; i++) { for (int j = 0; j <= len2; j++) { dp[i][j] = 0; } } for (int i = 1; i <= len1; i++) { for (int j = 1; j <= len2; 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]; } } else { dp[i][j] = 0; } } } return maxLen; } int main() { char s1[] = "abcdef"; char s2[] = "zcdemf"; printf("%d", longestCommonSubstring(s1, s2)); return 0; }
Attempts:
2 left
💡 Hint
Check the longest continuous matching substring between the two strings.
✗ Incorrect
The longest common substring between "abcdef" and "zcdemf" is "cde" which has length 3.
🧠 Conceptual
intermediate1:30remaining
Understanding the Difference Between Longest Common Substring and Longest Common Subsequence
Which statement correctly describes the difference between the longest common substring and the longest common subsequence of two strings?
Attempts:
2 left
💡 Hint
Think about whether the characters must be next to each other or can be spread out.
✗ Incorrect
Longest common substring needs the matching characters to be next to each other in both strings. Longest common subsequence allows characters to be in order but not necessarily contiguous.
🔧 Debug
advanced2:00remaining
Identify the Bug in Longest Common Substring Implementation
What error will the following C code produce when run, and why?
DSA C
int longestCommonSubstring(char *s1, char *s2) { int maxLen = 0; int len1 = strlen(s1); int len2 = strlen(s2); int dp[len1][len2]; for (int i = 0; i < len1; i++) { for (int j = 0; j < len2; j++) { dp[i][j] = 0; } } for (int i = 1; i <= len1; i++) { for (int j = 1; j <= len2; 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]; } } else { dp[i][j] = 0; } } } return maxLen; }
Attempts:
2 left
💡 Hint
Check the size of dp and the indices used to access it.
✗ Incorrect
The dp array is declared with size [len1][len2], but the code accesses dp[i][j] where i and j go up to len1 and len2 inclusive, causing out-of-bounds access and segmentation fault.
🚀 Application
advanced2:00remaining
Longest Common Substring in DNA Sequences
Given two DNA sequences, which approach is best to find the longest common substring efficiently?
Attempts:
2 left
💡 Hint
Consider time complexity and memory usage for large sequences.
✗ Incorrect
Dynamic programming with a 2D array efficiently computes the longest common substring in O(n*m) time, suitable for DNA sequences. Brute force and recursion are too slow, and hash sets are inefficient for substring matching.
🧠 Conceptual
expert1:30remaining
Time Complexity of Longest Common Substring Algorithm
What is the time complexity of the standard dynamic programming algorithm to find the longest common substring of two strings of lengths n and m?
Attempts:
2 left
💡 Hint
Consider the nested loops iterating over both strings.
✗ Incorrect
The algorithm uses two nested loops each running up to n and m, resulting in O(n * m) time complexity.