What is the Edit Distance (Levenshtein distance) between two strings?
It is the minimum number of single-character edits (insertions, deletions, or substitutions) required to change one string into the other.
Click to reveal answer
beginner
Which operations are considered in the Levenshtein distance calculation?
Insertion, deletion, and substitution of a single character.
Click to reveal answer
intermediate
What is the base case when computing Levenshtein distance using dynamic programming?
When one string is empty, the distance is the length of the other string (all insertions or deletions).
Click to reveal answer
intermediate
How does the dynamic programming table help in computing the Levenshtein distance?
It stores the minimum edit distances for all prefixes of the two strings, allowing reuse of previous results to build the solution efficiently.
Click to reveal answer
intermediate
What is the time complexity of the standard dynamic programming solution for Levenshtein distance?
O(m * n), where m and n are the lengths of the two strings.
Click to reveal answer
Which of the following operations is NOT part of the Levenshtein distance calculation?
AInsertion
BDeletion
CSubstitution
DTransposition
✗ Incorrect
Levenshtein distance considers insertion, deletion, and substitution only. Transposition is part of the Damerau-Levenshtein distance.
If one string is empty and the other has length 5, what is the Levenshtein distance between them?
A1
B5
C0
D10
✗ Incorrect
The distance equals the length of the non-empty string because all characters must be inserted or deleted.
What does the cell dp[i][j] represent in the dynamic programming table for Levenshtein distance?
ADistance between prefixes of length i and j
BDistance between full strings
CNumber of substitutions only
DLength of the longest common subsequence
✗ Incorrect
dp[i][j] stores the minimum edit distance between the first i characters of one string and the first j characters of the other.
What is the first step when filling the dynamic programming table for Levenshtein distance?
AFill the first row and first column
BFill the middle cell
CFill the diagonal
DFill the last row
✗ Incorrect
The first row and column represent distances when one string is empty, so they are initialized with incremental values.
What is the space complexity of the standard dynamic programming solution for Levenshtein distance?
AO(1)
BO(m + n)
CO(m * n)
DO(m^2 + n^2)
✗ Incorrect
The DP table requires space proportional to the product of the lengths of the two strings.
Explain how to compute the Levenshtein distance between two strings using dynamic programming.
Think about building solutions for smaller prefixes first.
You got /4 concepts.
Describe the role of base cases in the Levenshtein distance dynamic programming approach.
What happens when one string has zero length?
You got /3 concepts.
Practice
(1/5)
1. What does the Edit Distance (Levenshtein distance) between two strings measure?
easy
A. The number of matching characters in the same positions
B. The number of characters in the longer string
C. The minimum number of single-character edits to change one string into the other
D. The length difference between the two strings
Solution
Step 1: Understand the definition of Edit Distance
Edit Distance counts how many changes (insert, delete, replace) are needed to convert one string to another.
Step 2: Identify what is measured
It measures the minimum number of single-character edits required.
Final Answer:
The minimum number of single-character edits to change one string into the other -> Option C
Quick Check:
Edit Distance = minimum edits [OK]
Hint: Edit Distance counts minimal edits to match strings [OK]
Common Mistakes:
Confusing edit distance with string length
Thinking it counts only matching characters
Assuming it measures length difference only
2. Which of the following Go function signatures correctly represents a function to compute Levenshtein distance between two strings s1 and s2?
easy
A. func Levenshtein(s1, s2 string) string
B. func Levenshtein(s1, s2 string) int
C. func Levenshtein(s1 int, s2 int) string
D. func Levenshtein(s1, s2 int) int
Solution
Step 1: Identify input types for strings
The function must accept two strings as input parameters.
Step 2: Identify output type
The output is an integer representing the edit distance.
Final Answer:
func Levenshtein(s1, s2 string) int -> Option B
Quick Check:
Input: strings, Output: int [OK]
Hint: Function inputs are strings, output is int distance [OK]
Common Mistakes:
Using int instead of string for inputs
Returning string instead of int
Mixing input types incorrectly
3. What is the output of the following Go code snippet computing Levenshtein distance between "kitten" and "sitting"?
package main
import "fmt"
func main() {
s1 := "kitten"
s2 := "sitting"
dist := Levenshtein(s1, s2)
fmt.Println(dist)
}
func Levenshtein(a, b string) int {
m, n := len(a), len(b)
dp := make([][]int, m+1)
for i := range dp {
dp[i] = make([]int, n+1)
}
for i := 0; i <= m; i++ {
dp[i][0] = i
}
for j := 0; j <= n; j++ {
dp[0][j] = j
}
for i := 1; i <= m; i++ {
for j := 1; j <= n; j++ {
cost := 0
if a[i-1] != b[j-1] {
cost = 1
}
dp[i][j] = min(dp[i-1][j]+1, dp[i][j-1]+1, dp[i-1][j-1]+cost)
}
}
return dp[m][n]
}
func min(x, y, z int) int {
if x < y {
if x < z {
return x
}
return z
}
if y < z {
return y
}
return z
}
medium
A. 6
B. 4
C. 5
D. 3
Solution
Step 1: Understand the strings and operations
"kitten" to "sitting" requires substitutions k->s, e->i, plus insert g.
Hint: Count substitutions and insertions carefully [OK]
Common Mistakes:
Counting only substitutions
Ignoring insertions
Miscounting character positions
4. Identify the bug in this Go function computing Levenshtein distance:
func Levenshtein(a, b string) int {
m, n := len(a), len(b)
dp := make([][]int, m)
for i := range dp {
dp[i] = make([]int, n)
}
for i := 0; i < m; i++ {
dp[i][0] = i
}
for j := 0; j < n; j++ {
dp[0][j] = j
}
for i := 1; i < m; i++ {
for j := 1; j < n; j++ {
cost := 0
if a[i-1] != b[j-1] {
cost = 1
}
dp[i][j] = min(dp[i-1][j]+1, dp[i][j-1]+1, dp[i-1][j-1]+cost)
}
}
return dp[m-1][n-1]
}
medium
A. dp slice sizes should be m+1 and n+1, not m and n
B. The cost calculation should be outside the loops
C. The min function is missing
D. The return statement should be dp[0][0]
Solution
Step 1: Check dp slice initialization
dp must have size m+1 by n+1 to include empty string cases, but code uses m and n only.
Step 2: Understand impact of wrong sizes
Using m and n causes index out of range errors and incorrect base cases.
Final Answer:
dp slice sizes should be m+1 and n+1, not m and n -> Option A
Quick Check:
dp size = m+1, n+1 [OK]
Hint: dp array must cover empty prefixes with +1 size [OK]
Common Mistakes:
Initializing dp with wrong sizes
Misplacing cost calculation
Returning wrong dp cell
5. Given two strings s1 = "intention" and s2 = "execution", what is the minimum edit distance using Levenshtein algorithm? Consider insertions, deletions, and substitutions only.
hard
A. 6
B. 5
C. 8
D. 7
Solution
Step 1: Analyze differences between "intention" and "execution"
Compare characters and count edits: substitutions and insertions/deletions as needed.
Step 2: Calculate minimal edits
Minimum edits calculated are 6 total.
Final Answer:
6 -> Option A
Quick Check:
Levenshtein("intention", "execution") = 6 [OK]
Hint: Count substitutions plus insertions/deletions carefully [OK]
Common Mistakes:
Underestimating substitutions
Ignoring insertions/deletions
Confusing character positions
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.