What is the edit distance (Levenshtein distance) problem?

Why Interviewers Ask This

This question targets practical, hands-on experience with Data Structures & Algorithms. Interviewers want to see if you've worked with these concepts in real projects, not just read about them. Strong answers include concrete examples.

Answer

The Edit Distance problem finds the minimum number of single-character operations (insert, delete, substitute) to transform one string into another. Operations: Insert a character; Delete a character; Substitute a character. Example: "kitten" → "sitting" = 3 operations (substitute k→s, substitute e→i, insert g). DP solution: dp[i][j] = min operations to convert s1[0..i-1] to s2[0..j-1]. Recurrence: if s1[i-1] == s2[j-1]: dp[i][j] = dp[i-1][j-1] (no operation needed); else: dp[i][j] = 1 + min(dp[i-1][j] (delete), dp[i][j-1] (insert), dp[i-1][j-1] (substitute)). Base: dp[i][0] = i (delete i characters), dp[0][j] = j (insert j characters). Time: O(m×n). Space: O(m×n), optimized to O(min(m,n)) using two rows. Applications: (1) Spell checker — suggest corrections; (2) DNA sequence alignment; (3) Fuzzy string matching; (4) git diff; (5) Plagiarism detection; (6) Natural language processing. Hamming distance: edit distance considering only substitutions (strings must be equal length). Jaro-Winkler distance: better for short strings like names.

Pro Tip

Demonstrate both theoretical understanding and practical experience. Say what it is, then give an example of how you actually used it in a Data Structures & Algorithms codebase.