Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
Get started
Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
Experiences
Practice
Mock InterviewsTimed interview simulations with feedbackCoaching1:1 coaching with people at your target companiesSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Build my plan
Dynamic Programming for Strings
00:00
5 left

Dynamic Programming for Strings

HardPython

Problem

qXR may receive OCR text containing character substitutions, missing characters, or adjacent character swaps. Given a source string and a target string, compute the minimum cost to align the source with the target.

Allowed operations are:

  1. Insert one character into the source, consuming one target character.
  2. Delete one source character.
  3. Replace one source character with one target character. Matching characters cost zero.
  4. Transpose two adjacent source characters when they equal the next two target characters in reverse order.

Each operation has a caller-provided non-negative cost. The alignment proceeds from left to right, so a transposition consumes exactly two source and two target characters.

Formal Specification

Implement minimum_alignment_cost(source, target, insert_cost, delete_cost, replace_cost, transpose_cost). Return an integer representing the minimum alignment cost. The inputs are strings and integer operation costs.

Constraints

  • 0 <= len(source), len(target) <= 2000
  • 0 <= insert_cost, delete_cost, replace_cost, transpose_cost <= 10^6
  • Strings contain lowercase English letters
  • A transposition consumes exactly two adjacent characters from each string

Function Signature

def minimum_alignment_cost(source, target, insert_cost, delete_cost, replace_cost, transpose_cost):
Interviewer

Your question is Dynamic Programming for Strings. Start with the requirements in the Question tab.

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.
CodePython 3
Sign up free to run your codeLog inLn 2
Run your code to see test output here.