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 counted in calculating the Levenshtein distance?
Insertion, deletion, and substitution of a single character each count as one operation.
Click to reveal answer
intermediate
What is the base case when using dynamic programming to compute Levenshtein distance?
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 for Levenshtein distance get filled?
Each cell represents the minimum edit distance between prefixes of the two strings, computed using values from neighboring cells (top, left, and top-left).
Click to reveal answer
beginner
Why is Levenshtein distance useful in real life?
It helps in spell checking, DNA sequence analysis, and natural language processing by measuring how similar two strings are.
Click to reveal answer
Which of the following is NOT an operation counted in Levenshtein distance?
AInsertion
BDeletion
CSubstitution
DTransposition
✗ Incorrect
Levenshtein distance counts insertion, deletion, and substitution. Transposition is counted in a different distance called Damerau-Levenshtein.
What is the Levenshtein distance between "cat" and "cut"?
A1
B2
C3
D0
✗ Incorrect
Only one substitution (a -> u) is needed to change "cat" to "cut".
If one string is empty and the other has length 5, what is the Levenshtein distance?
A1
B5
C0
DCannot be determined
✗ Incorrect
You need 5 insertions or deletions to convert an empty string to a string of length 5.
In dynamic programming for Levenshtein distance, what does the cell dp[i][j] represent?
ADistance between prefixes of length i and j
BLength of the longer string
CNumber of substitutions only
DDistance between full strings
✗ 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.
Which real-life application uses Levenshtein distance?
ACalculating factorial
BSorting numbers
CSpell checking
DDrawing graphics
✗ Incorrect
Spell checking uses Levenshtein distance to find words close to a misspelled word.
Explain how dynamic programming helps compute the Levenshtein distance between two strings.
Think about how you compare smaller parts of the strings step by step.
You got /4 concepts.
Describe a real-life scenario where calculating the Levenshtein distance is useful and why.
Think about when you want to find words or sequences that are almost the same.
You got /4 concepts.
Practice
(1/5)
1. What does the Levenshtein distance between two strings measure?
easy
A. The length difference between the two strings
B. The number of matching characters in the same positions
C. The minimum number of single-character edits needed to change one string into the other
D. The number of vowels in both strings combined
Solution
Step 1: Understand the definition of Levenshtein distance
It measures how many single-character edits (insertions, deletions, or substitutions) are needed to convert one string into another.
Step 2: Compare with other options
Options B, C, and D do not describe edit distance but other unrelated string properties.
Final Answer:
The minimum number of single-character edits needed to change one string into the other -> Option C
Quick Check:
Levenshtein distance = minimum edits [OK]
Hint: Think of edits needed to transform one word into another [OK]
Common Mistakes:
Confusing edit distance with matching characters count
Thinking it measures length difference only
Assuming it counts only substitutions
2. Which of the following JavaScript function signatures correctly represents a function to compute Levenshtein distance between two strings s1 and s2?
easy
A. function levenshtein(s1, s2) { /* code */ }
B. function levenshtein(s1, s2, s3) { /* code */ }
C. function levenshtein() { /* code */ }
D. function levenshtein(s1) { /* code */ }
Solution
Step 1: Identify required parameters for Levenshtein function
The function needs exactly two strings to compare, so it should accept two parameters.
Step 2: Check options for correct parameter count
function levenshtein(s1, s2) { /* code */ } has two parameters (s1, s2), which is correct. Options A, B, and C have incorrect parameter counts.
Final Answer:
function levenshtein(s1, s2) { /* code */ } -> Option A
Quick Check:
Two strings needed as input [OK]
Hint: Levenshtein compares two strings, so function needs two parameters [OK]
Common Mistakes:
Using wrong number of parameters
Omitting parameters entirely
Adding unnecessary extra parameters
3. What is the output of the following JavaScript code snippet?
A. Change loops to run from 0 to len1-1 and len2-1
B. Replace Math.min with Math.max in dp calculation
C. Remove the initialization loops for dp first row and column
D. Change dp array initialization to size (len1+1) x (len2+1)
Solution
Step 1: Analyze dp array initialization
The dp array is initialized with size len1 x len2, but the loops access indices up to len1 and len2, causing out-of-bounds errors.
Step 2: Fix dp size to (len1+1) x (len2+1)
Allocating dp with one extra row and column prevents index errors and allows proper initialization of base cases.
Final Answer:
Change dp array initialization to size (len1+1) x (len2+1) -> Option D
Quick Check:
dp size must match loop bounds [OK]
Hint: dp size must be string length + 1 for indexing [OK]
Common Mistakes:
Not allocating dp with extra row/column
Using wrong loop ranges causing errors
Replacing min with max breaks logic
5. Given two strings s1 = 'intention' and s2 = 'execution', what is the Levenshtein distance between them? Use the concept of minimum edits (insertions, deletions, substitutions) to find the answer.
hard
A. 4
B. 6
C. 5
D. 7
Solution
Step 1: Analyze differences between 'intention' and 'execution'
Transform 'intention' to 'execution' involves substitutions and insertions: i->e, n->x, t->c, e->u, n->t, plus insert 'o'. Total edits count to 6.
Step 2: Count minimum edits carefully
Counting substitutions and insertions needed gives 6 as the minimum number of edits.
Final Answer:
6 -> Option B
Quick Check:
Minimum edits between 'intention' and 'execution' = 6 [OK]
Hint: Count each letter change and insertion carefully [OK]
Common Mistakes:
Underestimating substitutions needed
Ignoring insertions or deletions
Confusing with string length difference
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.