Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Edit Distance Algorithm

HardPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Edit Distance Algorithm. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Demandbase One may need to compare slightly different account or contact names during matching. Given two strings, compute their Levenshtein edit distance, the minimum number of single-character operations needed to transform the first string into the second.

Allowed operations are inserting one character, deleting one character, or replacing one character. Each operation has cost 1, and matching characters have cost 0.

Formal Specification

Implement edit_distance(word1, word2). The inputs are strings containing lowercase English letters. Return an integer representing the minimum edit cost. The function must use dynamic programming and should use O(min(m, n)) auxiliary space, where m and n are the input lengths.

Constraints

  • 0 <= len(word1), len(word2) <= 2000
  • Inputs contain only lowercase English letters
  • Insertion, deletion, and replacement each cost 1
  • The function must return the minimum edit cost

Function Signature

def edit_distance(word1, word2):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output