Coding · must-tier · asked at Amazon, Apple, Google, Meta, Microsoft, Netflix
Edit Distance
Problem
You are given two strings word1 and word2. In one operation you may insert a character, delete a character, or replace a character anywhere in word1. Return the fewest operations needed to turn word1 into word2.
0 <= len(word1), len(word2) <= 500- Both strings consist of lowercase English letters.
Examples
word1 = "saturday", word2 = "sunday"→3. Delete a, delete t, replace r→n.word1 = "planet", word2 = "plane"→1. Delete the trailing t.word1 = "", word2 = "xyz"→3. Only insertions are possible from an empty string.
Hints
- Look at the last characters of both strings. If they match, do you ever need to touch them?
- If they differ, the last operation must be exactly one of insert, delete, or replace. Each one leaves you with a smaller subproblem.
- 2D DP on prefix lengths:
dp[i][j]= distance betweenword1[:i]andword2[:j].
Last reviewed 8 September 2026
Locked
Approach · Solution (Python) · Complexity · Follow-ups · Company variations · Tests
The rest of this question — the worked approach, the Python solution with passing tests, complexity, follow-ups and how each company frames it — is part of the guides.
Get the full guide
Pick the company you're interviewing with. Each guide has every question tiered for that loop — one-time purchase, lifetime access.
Secure payment by Stripe · instant access · refund questions: [email protected]
Also in: Apple, Google, Meta, Microsoft, Netflix
Interviewing at more than one? All 6 guides for $499 · save $395
Not ready to buy? Get the free starter pack — 13 questions with full solutions.