Question presented to candidate: "Given two words, return the minimum number of operations to turn the first into the second, where an operation inserts, deletes or replaces one character. Explain your table, then code it in Python."
What a strong answer should cover:
- The state
dp[i][j]= fewest edits turning the first i characters ofword1into the first j ofword2. - The base cases
dp[i][0] = ianddp[0][j] = j(not zeros), and why. - The transition: equal characters copy the diagonal; otherwise 1 + min(diagonal = replace, up = delete, left = insert).
- O(m·n) time and space; O(min(m, n)) space with two rows when only the number is needed; walking back for the edit script.
- Python details: building the table with a list comprehension,
min()over three values, and thatdifflib.SequenceMatcheris not a Levenshtein distance.
Clarifying questions expected:
- Are all three operations allowed, and do they all cost 1?
- Does swapping two adjacent characters count as one edit?
- Do you need the edit operations or just the count?
Code / implementation expected: Yes — a bottom-up table in Python; mention the two-row optimisation and how to recover the operations.