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 Go code that calculates the Levenshtein distance between two strings?
DSA Go
package main import "fmt" func min(a, b, c int) int { if a < b { if a < c { return a } return c } if b < c { return b } return c } func levenshtein(a, b string) int { la := len(a) lb := len(b) dp := make([][]int, la+1) for i := range dp { dp[i] = make([]int, lb+1) } for i := 0; i <= la; i++ { dp[i][0] = i } for j := 0; j <= lb; j++ { dp[0][j] = j } for i := 1; i <= la; i++ { for j := 1; j <= lb; j++ { if a[i-1] == b[j-1] { dp[i][j] = dp[i-1][j-1] } else { dp[i][j] = min(dp[i-1][j]+1, dp[i][j-1]+1, dp[i-1][j-1]+1) } } } return dp[la][lb] } func main() { fmt.Println(levenshtein("kitten", "sitting")) }
Attempts:
2 left
💡 Hint
Think about the minimum number of edits (insertions, deletions, substitutions) to convert 'kitten' to 'sitting'.
✗ Incorrect
The Levenshtein distance between 'kitten' and 'sitting' is 3: substitute 'k'->'s', substitute 'e'->'i', and 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
Levenshtein distance counts the minimum edits needed using basic operations.
✗ Incorrect
Levenshtein distance counts insertions, deletions, and substitutions only. Transpositions are not counted in Levenshtein but in other metrics like Damerau-Levenshtein.
🔧 Debug
advancedIdentify the Error in Levenshtein Implementation
What error will the following Go code produce when calculating the Levenshtein distance?
DSA Go
package main import "fmt" func levenshtein(a, b string) int { la := len(a) lb := len(b) dp := make([][]int, la) for i := range dp { dp[i] = make([]int, lb) } for i := 0; i < la; i++ { dp[i][0] = i } for j := 0; j < lb; j++ { dp[0][j] = j } for i := 1; i < la; i++ { for j := 1; j < lb; j++ { if a[i-1] == b[j-1] { dp[i][j] = dp[i-1][j-1] } else { dp[i][j] = min(dp[i-1][j]+1, dp[i][j-1]+1, dp[i-1][j-1]+1) } } } return dp[la-1][lb-1] } func min(a, b, c int) int { if a < b { if a < c { return a } return c } if b < c { return b } return c } func main() { fmt.Println(levenshtein("abc", "yabd")) }
Attempts:
2 left
💡 Hint
Check the size of the dp array and the loop boundaries.
✗ Incorrect
The dp array is created with size la and lb, but loops and indexing use la and lb as if arrays had length la+1 and lb+1, causing index out of range error.
🚀 Application
advancedMinimum Edit Distance for Spell Checker
You want to find the closest word to 'speling' from the list ['spelling', 'spieling', 'selling', 'spring'] using Levenshtein distance. Which word will have the minimum distance?
Attempts:
2 left
💡 Hint
Calculate the number of edits needed to convert 'speling' to each word.
✗ Incorrect
The word 'spelling' differs by one insertion ('l') from 'speling', which is fewer edits than the others.
🧠 Conceptual
expertTime Complexity of Levenshtein Distance Algorithm
What is the time complexity of the classic dynamic programming algorithm for computing the Levenshtein distance between two strings of lengths m and n?
Attempts:
2 left
💡 Hint
Consider the size of the dp table and the nested loops.
✗ Incorrect
The algorithm fills a 2D table of size m+1 by n+1, so it runs in O(m * n) time.
