Challenge - 5 Problems
Levenshtein Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Levenshtein Distance Calculation
What is the output of the following Java code that calculates the Levenshtein distance between two strings?
DSA Java
public class Levenshtein { public static int levenshteinDistance(String s1, String s2) { int len1 = s1.length(); int len2 = s2.length(); int[][] dp = new int[len1 + 1][len2 + 1]; for (int i = 0; i <= len1; i++) { dp[i][0] = i; } for (int j = 0; j <= len2; j++) { dp[0][j] = j; } for (int i = 1; i <= len1; i++) { for (int j = 1; j <= len2; j++) { if (s1.charAt(i - 1) == s2.charAt(j - 1)) { dp[i][j] = dp[i - 1][j - 1]; } else { dp[i][j] = 1 + Math.min(dp[i - 1][j - 1], Math.min(dp[i - 1][j], dp[i][j - 1])); } } } return dp[len1][len2]; } public static void main(String[] args) { System.out.println(levenshteinDistance("kitten", "sitting")); } }
Attempts:
2 left
💡 Hint
Count the minimum edits (insert, delete, replace) to convert 'kitten' to 'sitting'.
✗ Incorrect
The Levenshtein distance between "kitten" and "sitting" is 3:
- Replace 'k' with 's'
- Replace 'e' with 'i'
- Insert 'g' at the end
🧠 Conceptual
intermediateUnderstanding Levenshtein Distance Operations
Which of the following operations are considered when calculating the Levenshtein distance between two strings?
Attempts:
2 left
💡 Hint
Think about basic edits to transform one string into another.
✗ Incorrect
Levenshtein distance counts the minimum number of insertions, deletions, and replacements needed to convert one string into another. Swapping or transposition is not included.
🔧 Debug
advancedIdentify the Bug in Levenshtein Distance Code
What error will the following Java code produce when calculating Levenshtein distance?
DSA Java
public static int levenshteinDistance(String s1, String s2) { int len1 = s1.length(); int len2 = s2.length(); int[][] dp = new int[len1][len2]; for (int i = 0; i <= len1; i++) { dp[i][0] = i; } for (int j = 0; j <= len2; j++) { dp[0][j] = j; } for (int i = 1; i <= len1; i++) { for (int j = 1; j <= len2; j++) { if (s1.charAt(i - 1) == s2.charAt(j - 1)) { dp[i][j] = dp[i - 1][j - 1]; } else { dp[i][j] = 1 + Math.min(dp[i - 1][j - 1], Math.min(dp[i - 1][j], dp[i][j - 1])); } } } return dp[len1][len2]; }
Attempts:
2 left
💡 Hint
Check the size of the dp array and the indices used.
✗ Incorrect
The dp array is declared with size [len1][len2], but the code accesses dp[len1][...] and dp[...][len2], which are out of bounds. The array should be sized [len1+1][len2+1].
❓ Predict Output
advancedLevenshtein Distance for Empty String Input
What is the output of this Java code when one input string is empty?
DSA Java
public class Levenshtein { public static int levenshteinDistance(String s1, String s2) { int len1 = s1.length(); int len2 = s2.length(); int[][] dp = new int[len1 + 1][len2 + 1]; for (int i = 0; i <= len1; i++) { dp[i][0] = i; } for (int j = 0; j <= len2; j++) { dp[0][j] = j; } for (int i = 1; i <= len1; i++) { for (int j = 1; j <= len2; j++) { if (s1.charAt(i - 1) == s2.charAt(j - 1)) { dp[i][j] = dp[i - 1][j - 1]; } else { dp[i][j] = 1 + Math.min(dp[i - 1][j - 1], Math.min(dp[i - 1][j], dp[i][j - 1])); } } } return dp[len1][len2]; } public static void main(String[] args) { System.out.println(levenshteinDistance("", "abc")); } }
Attempts:
2 left
💡 Hint
Distance equals length of the non-empty string when the other is empty.
✗ Incorrect
When one string is empty, the distance is the length of the other string because all characters must be inserted or deleted.
🧠 Conceptual
expertTime Complexity of Levenshtein Distance Algorithm
What is the time complexity of the classic dynamic programming solution to compute the Levenshtein distance between two strings of lengths m and n?
Attempts:
2 left
💡 Hint
Consider the size of the dp table and how many cells are filled.
✗ Incorrect
The algorithm fills a 2D table of size (m+1) x (n+1), so the time complexity is O(m * n).
