Compute the Minimal Edit Script Between Two Strings

Last updated: November 29, 2026

Quick Overview

Given two strings, return the minimum number of single-character insertions, deletions, and replacements needed to turn the first into the second, along with one actual sequence of operations that achieves it.

Microsoft
Coding & Algorithms
Software Engineer
Microsoft
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Medium

0

0

0 solved


Given two strings, return the minimum number of single-character insertions, deletions, and replacements needed to turn the first into the second, along with one actual sequence of operations that achieves it.

Word's Track Changes, Office spell check suggestions, and GitHub diffs all depend on edit scripts, so Microsoft interviewers extend the classic edit distance by asking for the operations themselves. They check the DP recurrence, a correct backtrack that is consistent with the table, and how you would scale it to long documents.

What the Interviewer Expects
  • States the recurrence and base cases clearly
  • Fills the table bottom-up and explains what dp[i][j] means
  • Reconstructs operations by backtracking with checks that match the recurrence
  • Verifies the script by applying it
  • Discusses O(min(n, m)) space for distance only and Hirschberg for the script
Key Topics to Cover
Dynamic programming
Levenshtein distance
Backtracking
Diff algorithms
Space optimization
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • How do you get the distance in O(min(n, m)) memory?
  • How would you produce the script for two 1 MB documents without an n by m table?
  • Diff tools work on lines, not characters: what changes?
  • How would you add transpositions (Damerau) for typo correction?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

Return (distance, ops) where ops is a list of keep, replace, delete, and insert steps that read a left to right and produce b. Each non-keep op costs 1. Any minimal script is acceptable....

High-Level Design

Let dp[i][j] be the cost to turn the first i characters of a into the first j of b. Base cases: dp[i][0] = i (delete everything) and dp[0][j] = j (insert everything). If the last characters ...


Submit Your Answer
Markdown supported