PracHub
QuestionsLearningGuidesInterview Prep
|Home/Coding & Algorithms/Amazon

Compute Edit Distance

Last updated: Jul 18, 2026

Quick Overview

Implement a function that returns the minimum insertions, deletions, and replacements needed to transform one lowercase string into another. Handle empty strings and inputs up to 500 characters with a polynomial-time approach.

  • medium
  • Amazon
  • Coding & Algorithms
  • Software Engineer

Compute Edit Distance

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

# Compute Edit Distance Given two strings, return the minimum number of single-character operations needed to transform the first string into the second. The permitted operations are: - insert one character, - delete one character, - replace one character. Each operation costs `1`. ## Function Contract ```python def edit_distance(word1: str, word2: str) -> int: ... ``` ## Constraints - `0 <= len(word1), len(word2) <= 500` - Both strings contain lowercase English letters. - Return an integer. - Your implementation should use polynomial time and must handle an empty input string. ## Examples ```text word1 = "horse" word2 = "ros" result = 3 ``` ```text word1 = "" word2 = "abc" result = 3 ```

Quick Answer: Implement a function that returns the minimum insertions, deletions, and replacements needed to transform one lowercase string into another. Handle empty strings and inputs up to 500 characters with a polynomial-time approach.

Related Interview Questions

  • Minimize Replacements So Equal Product Values Are Contiguous - Amazon (hard)
  • Find a Maximum-Sum Window in a Sparse Array - Amazon (hard)
  • Add One Source to Minimize Grid Inconvenience - Amazon (medium)
  • Implement a Half-Open Interval Range Module - Amazon (hard)
|Home/Coding & Algorithms/Amazon

Compute Edit Distance

Amazon logo
Amazon
Jul 15, 2026, 12:00 AM
mediumSoftware EngineerTechnical ScreenCoding & Algorithms
15
0
PracticeRead

Compute Edit Distance

Given two strings, return the minimum number of single-character operations needed to transform the first string into the second. The permitted operations are:

  • insert one character,
  • delete one character,
  • replace one character.

Each operation costs 1.

Function Contract

def edit_distance(word1: str, word2: str) -> int:
    ...

Constraints

  • 0 <= len(word1), len(word2) <= 500
  • Both strings contain lowercase English letters.
  • Return an integer.
  • Your implementation should use polynomial time and must handle an empty input string.

Examples

word1 = "horse"
word2 = "ros"
result = 3
word1 = ""
word2 = "abc"
result = 3

Submit Your Answer to Earn 20XP

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More Amazon•More Software Engineer•Amazon Software Engineer•Amazon Coding & Algorithms•Software Engineer Coding & Algorithms
PracHub

Master your tech interviews with 8,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • AI Coding Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.