0
0
DSA Cprogramming~20 mins

Longest Common Subsequence in DSA C - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
LCS Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of LCS length calculation
What is the output of the following C code that calculates the length of the Longest Common Subsequence (LCS) between two strings?
DSA C
int lcs_length(char *X, char *Y) {
    int m = strlen(X);
    int n = strlen(Y);
    int L[m+1][n+1];
    for (int i = 0; i <= m; i++) {
        for (int j = 0; j <= n; j++) {
            if (i == 0 || j == 0)
                L[i][j] = 0;
            else if (X[i-1] == Y[j-1])
                L[i][j] = L[i-1][j-1] + 1;
            else
                L[i][j] = (L[i-1][j] > L[i][j-1]) ? L[i-1][j] : L[i][j-1];
        }
    }
    return L[m][n];
}

int main() {
    char X[] = "AGGTAB";
    char Y[] = "GXTXAYB";
    printf("%d", lcs_length(X, Y));
    return 0;
}
A5
B7
C6
D4
Attempts:
2 left
💡 Hint
Trace the matching characters between the two strings and count the longest sequence.
Predict Output
intermediate
2:00remaining
Output of LCS sequence reconstruction
What is the output of the following C code that reconstructs and prints the Longest Common Subsequence (LCS) between two strings?
DSA C
#include <stdio.h>
#include <string.h>

void print_lcs(char *X, char *Y) {
    int m = strlen(X);
    int n = strlen(Y);
    int L[m+1][n+1];
    for (int i = 0; i <= m; i++)
        for (int j = 0; j <= n; j++)
            L[i][j] = 0;
    for (int i = 1; i <= m; i++) {
        for (int j = 1; j <= n; j++) {
            if (X[i-1] == Y[j-1])
                L[i][j] = L[i-1][j-1] + 1;
            else
                L[i][j] = (L[i-1][j] > L[i][j-1]) ? L[i-1][j] : L[i][j-1];
        }
    }
    int index = L[m][n];
    char lcs[index+1];
    lcs[index] = '\0';
    int i = m, j = n;
    while (i > 0 && j > 0) {
        if (X[i-1] == Y[j-1]) {
            lcs[index-1] = X[i-1];
            i--;
            j--;
            index--;
        } else if (L[i-1][j] > L[i][j-1])
            i--;
        else
            j--;
    }
    printf("%s", lcs);
}

int main() {
    char X[] = "ABCBDAB";
    char Y[] = "BDCABA";
    print_lcs(X, Y);
    return 0;
}
ABCBA
BBDAB
CBCAB
DBDCB
Attempts:
2 left
💡 Hint
Trace back from the bottom-right of the LCS table to find the sequence.
🧠 Conceptual
advanced
1:00remaining
Time complexity of LCS dynamic programming solution
What is the time complexity of the classic dynamic programming solution to find the length of the Longest Common Subsequence of two strings of lengths m and n?
AO(m + n)
BO(m^2 + n^2)
CO(m * n)
DO(2^(m+n))
Attempts:
2 left
💡 Hint
Consider the nested loops iterating over both strings.
🔧 Debug
advanced
2:00remaining
Identify the error in LCS length calculation code
What error will the following C code produce when trying to calculate the LCS length?
DSA C
int lcs_length(char *X, char *Y) {
    int m = strlen(X);
    int n = strlen(Y);
    int *L = malloc((m+1)*(n+1)*sizeof(int));
    for (int i = 0; i <= m; i++) {
        for (int j = 0; j <= n; j++) {
            if (i == 0 || j == 0)
                L[i*(n+1)+j] = 0;
            else if (X[i-1] == Y[j-1])
                L[i*(n+1)+j] = L[(i-1)*(n+1)+(j-1)] + 1;
            else {
                int a = L[(i-1)*(n+1)+j];
                int b = L[i*(n+1)+(j-1)];
                L[i*(n+1)+j] = (a > b) ? a : b;
            }
        }
    }
    int result = L[m*(n+1)+n];
    free(L);
    return result;
}

int main() {
    char X[] = "ABC";
    char Y[] = "AC";
    printf("%d", lcs_length(X, Y));
    return 0;
}
ANo error, outputs 2
BSegmentation fault due to invalid memory access
CRuntime error: double free detected
DCompilation error: missing #include <stdlib.h>
Attempts:
2 left
💡 Hint
Check if all required headers are included for used functions.
🚀 Application
expert
3:00remaining
Number of distinct LCS sequences
Given two strings X = "ABCBDAB" and Y = "BDCABA", how many distinct Longest Common Subsequences do they have?
A3
B4
C2
D1
Attempts:
2 left
💡 Hint
Consider all sequences of length equal to the LCS length and check which are distinct.