Prepin
Log in

Modification Distance

Approach / Explanation

Edit Distance is a term in computer science that refers to the least number of operations—such as insertions, deletions, and substitutions—needed to change one word into another. To tackle this type of question, the interviewee should have a solid understanding of dynamic programming, as the typical method for solving it involves a matrix-based dynamic programming technique. Important aspects to address when responding to the question include defining what edit distance is, describing the method required to calculate it, and providing an example to demonstrate the procedure.

Suggested Answer

Edit Distance, often referred to as Levenshtein distance, quantifies the difference between two sequences. In computer science, particularly when working with strings, the edit distance indicates the smallest number of operations (insertions, deletions, substitutions) required to convert one string into another. The typical approach to solving this problem involves using a tabulation-based dynamic programming method. We create a 2D matrix where the cell located at the i-th row and j-th column signifies the edit distance between the first i characters of the first string and the first j characters of the second string. For instance, to convert the word “kitten” into “sitting”, we can determine the edit distance as follows: Replace 'k' with 's' Add 'i' No change for 't' No change for 't' Replace 'e' with 'i' Replace 'n' with 'g' Add 'g' Thus, the edit distance between "kitten" and "sitting" is 5.

Alternative Answer

Edit Distance can also be addressed using a recursive approach. The fundamental concept mirrors that of dynamic programming, but the key distinction lies in how subproblem solutions are stored and reused. For two strings, s1 and s2, our objective is to calculate the edit distance DP[i][j] , where i and j represent the lengths of s1 and s2, respectively. We can consider three possible operations: If the last characters are identical, we can disregard them and calculate the count for the remaining substrings. Thus, DP[i][j] = DP[i-1][j-1] . If the last characters differ, we remove the last character from one of the strings and recursively compute the edit distance. The minimum of these three operations, plus 1, will give us our answer. DP[i][j] = min(DP[i][j - 1], # Remove from s1 DP[i - 1][j], # Remove from s2 DP[i - 1][j - 1]) # Replace in s1 or s2 This recursive method results in overlapping subproblems, so we can use a memoization technique to store the results of these subproblems for later reuse.

Question Details

Difficulty & Category

Medium

Companies

Google

Product

For CandidatesFor RecruitersCompaniesBrowse JobsInterview QuestionsDeep ProfileSkill AssessmentOpportunity Matching
Prepin.ai

© 2026 Prepin LLC · Philadelphia, PA · All rights reserved.