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 C++ code that calculates the length of the longest common substring between two strings?
DSA C++
int longestCommonSubstring(std::string s1, std::string s2) { int m = s1.size(); int n = s2.size(); int maxLen = 0; std::vector<std::vector<int>> dp(m + 1, std::vector<int>(n + 1, 0)); for (int i = 1; i <= m; i++) { for (int j = 1; j <= n; j++) { if (s1[i - 1] == s2[j - 1]) { dp[i][j] = dp[i - 1][j - 1] + 1; maxLen = std::max(maxLen, dp[i][j]); } } } return maxLen; } int main() { std::string a = "abcde"; std::string b = "abfce"; std::cout << longestCommonSubstring(a, b); return 0; }
Attempts:
2 left
💡 Hint
Check the longest sequence of matching characters in order between the two strings.
✗ Incorrect
The longest common substring between "abcde" and "abfce" is "ab" which has length 2.
❓ Predict Output
intermediateLongest Common Substring Length for Overlapping Strings
What will be the output of this C++ function call for longest common substring length?
DSA C++
std::cout << longestCommonSubstring("banana", "ananas");
Attempts:
2 left
💡 Hint
Look for the longest continuous matching substring.
✗ Incorrect
The longest common substring is "anana" which has length 5.
🔧 Debug
advancedIdentify the Error in Longest Common Substring Implementation
What error will this code produce when trying to find the longest common substring length?
DSA C++
int longestCommonSubstring(std::string s1, std::string s2) { int m = s1.size(); int n = s2.size(); int maxLen = 0; std::vector<std::vector<int>> dp(m, std::vector<int>(n, 0)); for (int i = 1; i <= m; i++) { for (int j = 1; j <= n; j++) { if (s1[i - 1] == s2[j - 1]) { dp[i][j] = dp[i - 1][j - 1] + 1; maxLen = std::max(maxLen, dp[i][j]); } } } return maxLen; }
Attempts:
2 left
💡 Hint
Check the size of the dp vector and the indices used to access it.
✗ Incorrect
The dp vector is sized 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 access.
🧠 Conceptual
advancedTime Complexity of Longest Common Substring Algorithm
What is the time complexity of the dynamic programming approach to find the longest common substring of two strings of lengths m and n?
Attempts:
2 left
💡 Hint
Consider the nested loops iterating over both strings.
✗ Incorrect
The algorithm uses two nested loops each running up to m and n, resulting in O(m * n) time complexity.
🚀 Application
expertLongest Common Substring Result After Multiple Updates
Given the following sequence of operations on two strings, what is the length of their longest common substring after all operations?
DSA C++
std::string s1 = "abcdefg"; std::string s2 = "xyzabcde"; // Operation 1: Remove last character from s1 s1.pop_back(); // Operation 2: Append 'f' to s2 s2.push_back('f'); // Operation 3: Replace first character of s1 with 'x' s1[0] = 'x'; int result = longestCommonSubstring(s1, s2); std::cout << result;
Attempts:
2 left
💡 Hint
Apply each operation step-by-step and then find the longest common substring.
✗ Incorrect
After operations, s1 = "xbcdef" and s2 = "xyzabcdef". The longest common substring is "bcdef" with length 5.
