Compute the minimum insertions, deletions and substitutions to turn one word into another with a 2-D DP in O(m·n), then cut space to one row. FAANG hard.
The problem
Return the minimum number of single-character insertions, deletions or substitutions needed to turn a into b.
Examples
Example 1
Input
edit_distance('horse', 'ros')Expected output
3
Example 2
Input
edit_distance('intention', 'execution')Expected output
5
+ 4 hidden tests on Submit.
Edge cases to ask about
- Empty strings
- Identical strings
Hints
0/3How an interviewer scores this
0/9Your code runs in real CPython inside your browser — nothing is sent anywhere. The first run downloads the interpreter (about 6 MB, once). Your code is saved on this device as you type.
Complexity Lab
What does this cost as n grows?
Interviewers score the analysis as much as the code. Commit to an answer first — then check it, and measure your code against the optimal one at growing input sizes.
Pick both to reveal the answer.
Measure it
Runs the function on inputs of size 250 up to 16,000 and records the time and peak memory. Slow solutions stop early — a short curve is itself the answer.
From brute force to optimal
The progression an interviewer wants to hear, one step at a time.
| Approach | Time | Space | Idea |
|---|---|---|---|
| Plain recursion | O(3^(m+n)) | O(m + n) | |
| 2-D DP table | O(m · n) | O(m · n) | dp[i][j] = distance between prefixes a[:i] and b[:j]. |
| bestRolling one row | O(m · n) | O(n) | Each row only needs the previous one. |
Walkthrough of the optimal approach (try it yourself first)
dp[i][j] = distance between the first i characters of a and the first j of b. Base cases: turning a prefix into the empty string takes i deletions (dp[i][0] = i, dp[0][j] = j). If the last characters match, carry dp[i−1][j−1]; otherwise take 1 + the best of delete, insert and replace.
Each row depends only on the row above, so two rows (O(n) space) are enough.
Complexity: O(m · n) time, O(n) space. Every (i, j) prefix pair is computed once; only the previous row is kept.
Reveal the reference solution
def edit_distance(a, b): prev = list(range(len(b) + 1)) for i in range(1, len(a) + 1): cur = [i] + [0] * len(b) for j in range(1, len(b) + 1): if a[i - 1] == b[j - 1]: cur[j] = prev[j - 1] else: cur[j] = 1 + min(prev[j], # delete cur[j - 1], # insert prev[j - 1]) # replace prev = cur return prev[-1]
Follow-ups interviewers ask
- Reconstruct the actual edit operations.
- Only insertions and deletions allowed.
Frequently asked interview questions
Core interview concepts, complexities, and follow-ups scored by hiring teams.
What is the time complexity of Edit Distance (Levenshtein) in Python?
The optimal solution runs in O(m · n) time and O(n) auxiliary space. Every (i, j) prefix pair is computed once; only the previous row is kept.
What is the brute-force approach, and how do you optimise it?
Plain recursion: O(3^(m+n)) time, O(m + n) space. 2-D DP table: O(m · n) time, O(m · n) space. dp[i][j] = distance between prefixes a[:i] and b[:j]. Rolling one row: O(m · n) time, O(n) space. Each row only needs the previous one.
What follow-up questions do interviewers ask about Edit Distance (Levenshtein)?
Reconstruct the actual edit operations. Only insertions and deletions allowed.
