0
0
DSA Cprogramming~20 mins

Edit Distance Problem Levenshtein in DSA C - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Levenshtein Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Levenshtein Distance Calculation
What is the output of the following C code that calculates the Levenshtein distance between two strings?
DSA C
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int min(int a, int b, int c) {
    int m = a < b ? a : b;
    return m < c ? m : c;
}

int levenshtein(const char *s1, const char *s2) {
    int len1 = strlen(s1);
    int len2 = strlen(s2);
    int *dp = malloc((len1 + 1) * (len2 + 1) * sizeof(int));
    for (int i = 0; i <= len1; i++) {
        for (int j = 0; j <= len2; j++) {
            if (i == 0) dp[j] = j;
            else if (j == 0) dp[i * (len2 + 1)] = i;
            else {
                int cost = (s1[i - 1] == s2[j - 1]) ? 0 : 1;
                int deletion = dp[(i - 1) * (len2 + 1) + j] + 1;
                int insertion = dp[i * (len2 + 1) + j - 1] + 1;
                int substitution = dp[(i - 1) * (len2 + 1) + j - 1] + cost;
                dp[i * (len2 + 1) + j] = min(deletion, insertion, substitution);
            }
        }
    }
    int result = dp[len1 * (len2 + 1) + len2];
    free(dp);
    return result;
}

int main() {
    printf("%d\n", levenshtein("kitten", "sitting"));
    return 0;
}
A3
B4
C2
D5
Attempts:
2 left
💡 Hint
Recall the minimum number of edits (insertions, deletions, substitutions) to convert 'kitten' to 'sitting'.
🧠 Conceptual
intermediate
1:30remaining
Understanding Levenshtein Distance Operations
Which of the following operations are considered in the Levenshtein distance calculation?
AInsertion, Swap, Transposition
BInsertion, Deletion, Substitution
CSubstitution, Transposition, Swap
DInsertion, Deletion, Transposition
Attempts:
2 left
💡 Hint
Think about the basic edit operations allowed to transform one string into another.
Predict Output
advanced
1:30remaining
Result of Levenshtein Distance with Empty String
What is the output of this C code snippet calculating the Levenshtein distance between an empty string and "abc"?
DSA C
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int min(int a, int b, int c) {
    int m = a < b ? a : b;
    return m < c ? m : c;
}

int levenshtein(const char *s1, const char *s2) {
    int len1 = strlen(s1);
    int len2 = strlen(s2);
    int *dp = malloc((len1 + 1) * (len2 + 1) * sizeof(int));
    for (int i = 0; i <= len1; i++) {
        for (int j = 0; j <= len2; j++) {
            if (i == 0) dp[j] = j;
            else if (j == 0) dp[i * (len2 + 1)] = i;
            else {
                int cost = (s1[i - 1] == s2[j - 1]) ? 0 : 1;
                int deletion = dp[(i - 1) * (len2 + 1) + j] + 1;
                int insertion = dp[i * (len2 + 1) + j - 1] + 1;
                int substitution = dp[(i - 1) * (len2 + 1) + j - 1] + cost;
                dp[i * (len2 + 1) + j] = min(deletion, insertion, substitution);
            }
        }
    }
    int result = dp[len1 * (len2 + 1) + len2];
    free(dp);
    return result;
}

int main() {
    printf("%d\n", levenshtein("", "abc"));
    return 0;
}
A0
B1
C3
D2
Attempts:
2 left
💡 Hint
Consider how many insertions are needed to convert an empty string to "abc".
🔧 Debug
advanced
2:00remaining
Identify the Bug in Levenshtein Distance Code
What error will this C code produce when calculating the Levenshtein distance between "abc" and "yabd"?
DSA C
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int min(int a, int b, int c) {
    int m = a < b ? a : b;
    return m < c ? m : c;
}

int levenshtein(const char *s1, const char *s2) {
    int len1 = strlen(s1);
    int len2 = strlen(s2);
    int *dp = malloc((len1 + 1) * (len2 + 1) * sizeof(int));
    for (int i = 0; i <= len1; i++) {
        for (int j = 0; j <= len2; j++) {
            if (i == 0) dp[j] = j;
            else if (j == 0) dp[i * (len2 + 1)] = i;
            else {
                int cost = (s1[i - 1] == s2[j - 1]) ? 0 : 1;
                int deletion = dp[(i - 1) * (len2 + 1) + j] + 1;
                int insertion = dp[i * (len2 + 1) + j - 1] + 1;
                int substitution = dp[(i - 1) * (len2 + 1) + j - 1] + cost;
                dp[i * (len2 + 1) + j] = min(deletion, insertion, substitution);
            }
        }
    }
    int result = dp[len1 * (len2 + 1) + len2];
    free(dp);
    return result;
}

int main() {
    printf("%d\n", levenshtein("abc", "yabd"));
    return 0;
}
AInfinite loop in nested for loops
BCorrect output 2
CCompilation error due to missing header
DSegmentation fault due to incorrect dp indexing
Attempts:
2 left
💡 Hint
Check how dp array is accessed when i or j is zero.
🧠 Conceptual
expert
1:30remaining
Time Complexity of Levenshtein Distance Algorithm
What is the time complexity of the classic dynamic programming algorithm to compute the Levenshtein distance between two strings of lengths m and n?
AO(m * n)
BO(m + n)
CO(2^(m+n))
DO(m^2 + n^2)
Attempts:
2 left
💡 Hint
Consider the size of the DP table and how many cells are computed.