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 this C# code that calculates the Levenshtein distance between "kitten" and "sitting"?
DSA C#
using System; class Program { static int LevenshteinDistance(string s, string t) { int n = s.Length; int m = t.Length; int[,] d = new int[n + 1, m + 1]; for (int i = 0; i <= n; i++) d[i, 0] = i; for (int j = 0; j <= m; j++) d[0, j] = j; for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { int cost = (s[i - 1] == t[j - 1]) ? 0 : 1; d[i, j] = Math.Min(Math.Min(d[i - 1, j] + 1, d[i, j - 1] + 1), d[i - 1, j - 1] + cost); } } return d[n, m]; } static void Main() { Console.WriteLine(LevenshteinDistance("kitten", "sitting")); } }
Attempts:
2 left
💡 Hint
Count the minimum edits (insertions, deletions, substitutions) to convert "kitten" to "sitting".
✗ Incorrect
The Levenshtein distance between "kitten" and "sitting" is 3. The edits are: substitute 'k' with 's', substitute 'e' with 'i', insert 'g' at the end.
🧠 Conceptual
intermediateUnderstanding Levenshtein Distance Matrix Initialization
In the Levenshtein distance algorithm, why do we initialize the first row and first column of the matrix with incremental values?
Attempts:
2 left
💡 Hint
Think about how many edits it takes to convert an empty string to a string of length n.
✗ Incorrect
The first row and column represent the cost of converting an empty string to prefixes of the other string, which requires insertions or deletions equal to the prefix length.
🔧 Debug
advancedIdentify the Bug in Levenshtein Distance Implementation
What error will this C# code produce when calculating the Levenshtein distance between "abc" and "yabd"?
DSA C#
using System; class Program { static int LevenshteinDistance(string s, string t) { int n = s.Length; int m = t.Length; int[,] d = new int[n + 1, m + 1]; for (int i = 0; i <= n; i++) d[i, 0] = i; for (int j = 0; j <= m; j++) d[0, j] = j; for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { int cost = (s[i - 1] == t[j - 1]) ? 0 : 1; d[i, j] = Math.Min(Math.Min(d[i - 1, j] + 1, d[i, j - 1] + 1), d[i - 1, j - 1] + cost); } } return d[n, m]; } static void Main() { Console.WriteLine(LevenshteinDistance("abc", "yabd")); } }
Attempts:
2 left
💡 Hint
Check the size of the matrix and the loop boundaries carefully.
✗ Incorrect
The matrix is declared with size [n, m], but loops run up to n and m inclusive, causing index out of range errors when accessing d[i, j].
🚀 Application
advancedMinimum Edit Operations to Convert Strings
Given the strings "flaw" and "lawn", what is the minimum number of edit operations (insertions, deletions, substitutions) needed to convert "flaw" into "lawn"?
Attempts:
2 left
💡 Hint
Try to align the characters and count the minimum edits.
✗ Incorrect
The minimum edits are 2: delete 'f' and insert 'n' at the end.
❓ Predict Output
expertOutput of Levenshtein Distance with Empty String
What is the output of this C# code that calculates the Levenshtein distance between an empty string and "algorithm"?
DSA C#
using System; class Program { static int LevenshteinDistance(string s, string t) { int n = s.Length; int m = t.Length; int[,] d = new int[n + 1, m + 1]; for (int i = 0; i <= n; i++) d[i, 0] = i; for (int j = 0; j <= m; j++) d[0, j] = j; for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { int cost = (s[i - 1] == t[j - 1]) ? 0 : 1; d[i, j] = Math.Min(Math.Min(d[i - 1, j] + 1, d[i, j - 1] + 1), d[i - 1, j - 1] + cost); } } return d[n, m]; } static void Main() { Console.WriteLine(LevenshteinDistance("", "algorithm")); } }
Attempts:
2 left
💡 Hint
Consider how many insertions are needed to convert an empty string to "algorithm".
✗ Incorrect
Since the first string is empty, the distance equals the length of the second string, which is 9.
