Reported September 2026
Amazondynamic programming

Edit Distance

Reported by candidates from Amazon's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Amazon OA. Under 2s to a working solution.
Founder's read

Amazon reported this Edit Distance question in September 2026, and the whole solution hinges on a 2D DP table. Rows are prefixes of word1, columns are prefixes of word2, and each cell holds the cheapest way to turn one prefix into the other. If you've got an Amazon OA coming up, expect to build that grid from scratch under pressure. The idea is simple once you see it, but people blank on the recurrence. If that happens live, StealthCoder is the invisible safety net that reads the problem and hands you the solution while the proctor sees nothing.

The problem

Given two strings word1 and word2, return the minimum number of single-character operations needed to transform word1 into word2.
Each operation is one of the following:
Insert one character.
Delete one character.
Replace one character.

Function
minDistance(word1: String, word2: String) → int

Examples
Example 1
word1 = "horse"
word2 = "ros"
return = 3
Replace h with r, delete the second r, and delete e.
Example 2
word1 = "intention"
word2 = "execution"
return = 5
A minimum transformation uses five insert, delete, or replace operations.
Example 3
word1 = ""
word2 = "abc"
return = 3
Insert a, b, and c.

Constraints
0 <= word1.length <= 500.
0 <= word2.length <= 500.
Both strings contain only lowercase English letters.

Reported by candidates. Source: FastPrep

Pattern and pitfall

Define dp[i][j] as the minimum operations to turn the first i characters of word1 into the first j characters of word2. Base cases: dp[i][0] = i (delete everything) and dp[0][j] = j (insert everything). If word1[i-1] equals word2[j-1], copy dp[i-1][j-1]. Otherwise take 1 + min(dp[i-1][j] for delete, dp[i][j-1] for insert, dp[i-1][j-1] for replace). Answer is dp[m][n]. The common pitfall is off-by-one indexing between the table and the strings, and forgetting the empty-string case from Example 3. With lengths up to 500, O(m*n) is fine. You can compress to two rows if asked. If the recurrence slips away mid-assessment, StealthCoder is your hedge, but trace Example 1 by hand first and the grid will come back.

If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.

If this hits your live OA

You can drill Edit Distance cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it.

Get StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as edit distance. If you have time before the OA, drill that.

⏵ The honest play

You've seen the question. Make sure you actually pass Amazon's OA.

Amazon reuses patterns across OAs. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Edit Distance FAQ

What's the trick to Edit Distance?+

Build a 2D table where dp[i][j] is the cost to convert the first i letters of word1 into the first j letters of word2. Matching characters copy the diagonal. Mismatches take 1 plus the minimum of the left, top, and diagonal cells.

How hard is this Amazon OA question really?+

It's a classic medium-to-hard DP. The code is short, around 15 lines, but you have to derive the recurrence. If you've seen it once, it's easy. If you haven't, the three operations map to three neighboring cells, which unlocks it fast.

What do the three operations map to in the table?+

Delete is dp[i-1][j], insert is dp[i][j-1], and replace is dp[i-1][j-1]. Each costs one extra step on top of that neighbor's value. A matching character costs nothing extra and just takes the diagonal.

What edge cases should I test?+

Test an empty word1 with a non-empty word2, which is Example 3 and returns the length of word2. Test the reverse too. Also test identical strings, which should return 0, and two strings with no shared letters, which returns the longer length.

How do I prepare in 48 hours?+

Code Edit Distance by hand twice, filling the grid for horse and ros on paper. Then write Longest Common Subsequence, since it uses the same table shape. Practice setting base cases first, because that's where most off-by-one bugs start.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Amazon.

OA at Amazon?
Invisible during screen share
Get it