Minimum Edits to Correct a Misspelled Search Query
Last updated: November 29, 2026
Quick Overview
Given a typed word and a candidate correction, return the minimum number of single-character insertions, deletions, and substitutions needed to turn one into the other.
0
0
0 solved
Given a typed word and a candidate correction, return the minimum number of single-character insertions, deletions, and substitutions needed to turn one into the other.
Framed around Google's did-you-mean spelling correction, this is a core two-dimensional DP question. Interviewers look for a clear recurrence, correct base cases, and space optimization, and usually push into how you would use it against a large dictionary.
What the Interviewer Expects
- States the recurrence with insert, delete, and substitute cases
- Sets base cases for empty prefixes
- Fills the table in a valid order
- Reduces memory to two rows (O(min(m, n)))
- Explains why this is too slow to run against every dictionary word and what to do instead
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- Only compute whether the distance is at most k; can you do better than O(mn)?
- Add adjacent transposition as a single edit (Damerau-Levenshtein).
- Weight substitutions by keyboard distance; how does the DP change?
- Reconstruct the actual sequence of edits.
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
- Input: strings
a(length m) andb(length n). - Output: Levenshtein distance.
- Unit cost per insert, delete, substitute....
High-Level Design
Let D[i][j] be the distance between a[:i] and b[:j]. Base: D[i][0] = i (delete everything), D[0][j] = j (insert everything). If a[i-1] == b[j-1], D[i][j] = D[i-1][j-1]; otherwise it is 1...