0
0
DSA Cprogramming~20 mins

Longest Common Substring in DSA C - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Longest Common Substring Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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;
}
A3
B2
C4
D1
Attempts:
2 left
💡 Hint
Check the longest continuous matching substring between the two strings.
🧠 Conceptual
intermediate
1: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?
ALongest common subsequence requires characters to be contiguous in both strings, while longest common substring does not.
BLongest common substring requires characters to be contiguous in both strings, while longest common subsequence does not.
CBoth require characters to be contiguous in both strings.
DNeither requires characters to be contiguous in both strings.
Attempts:
2 left
💡 Hint
Think about whether the characters must be next to each other or can be spread out.
🔧 Debug
advanced
2: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;
}
ASegmentation fault due to accessing dp out of bounds.
BCompilation error due to missing header files.
CReturns 0 always because dp is never updated.
DRuns correctly and returns the longest common substring length.
Attempts:
2 left
💡 Hint
Check the size of dp and the indices used to access it.
🚀 Application
advanced
2:00remaining
Longest Common Substring in DNA Sequences
Given two DNA sequences, which approach is best to find the longest common substring efficiently?
AUse recursion without memoization to explore all substring matches.
BUse a brute force approach checking all substrings of one sequence in the other.
CUse dynamic programming with a 2D array to track matching substrings.
DUse a hash set to store all substrings of the first sequence and check the second.
Attempts:
2 left
💡 Hint
Consider time complexity and memory usage for large sequences.
🧠 Conceptual
expert
1: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?
AO(max(n, m))
BO(n + m)
CO(n^2 * m^2)
DO(n * m)
Attempts:
2 left
💡 Hint
Consider the nested loops iterating over both strings.