Hard · Algorithms · Topics: dynamic programming, string
The problem
You're on the team building the "spell-check + auto-fix" backend for a code-editor plugin (think: quick-fix suggestions when a variable name is misspelled, or a git-diff-style similarity scorer). Given two strings word1 and word2, you need to compute the minimum number of operations required to convert word1 into word2. The three allowed operations are:
Insert a character
Delete a character
Replace a character
This exact metric (edit distance / Levenshtein distance) is also what powers DNA sequence alignment tools and "did you mean" search corrections.
(Always mention these edge cases — empty strings, identical strings, single-char strings — before coding.)
How interviewers phrase it
"Given two words, find the minimum number of insertions, deletions, and substitutions to convert one into the other."
"How similar are these two strings?" (disguised framing — they want edit distance)
"Design a spell-checker's suggestion scorer" (system-design-flavored disguise of the same core DP)
Clarifying questions to ask
Are all three operations (insert/delete/replace) allowed, or only a subset? (Some variants only allow insert+delete — that changes the problem to LCS-based edit distance.)
Do all operations cost the same (unit cost), or are costs weighted differently (e.g., replace costs 2)? This changes the DP transition slightly.
Are the strings case-sensitive?
What's the expected length of the strings? (If very large, O(n·m) might still be fine since it's polynomial, but they may ask about space optimization.)
Do I need to just return the count, or also reconstruct the sequence of operations?
Follow-ups to expect
"Can you optimize space?"
"Can you print the actual sequence of operations, not just the count?"
"What if operations have different costs?" (e.g., insert=1, delete=1, replace=2)
"What if only insert and delete are allowed (no replace)?"
"Can you solve it recursively with memoization first before jumping to bottom-up?"
"What's the relation between this and LCS (Longest Common Subsequence)?"
Practise questions like this with a real engineer
On Practick another engineer interviews you live for 60 minutes on a question from this bank. They have the full interviewer guide beside the call: the expected answer to every follow-up, hints to give if you stall, and a scoring rubric. You get the model solution once the session settles. It is free: interview someone for an hour and you have earned your own.