Skip to main content

Any software role

Coding problem: Fewest Edits Between Strings. Return the minimum number of single-character edits that turn the string `a` into the string `b`. One edit inserts a character, deletes a character, or replaces one character with another.

TechnicalSeniorTests: Problem solving
Drill this questionCreate a free account to answer out loud with Ava and get feedback.

What the interviewer is looking for

  • What a strong solution looks like: DP table d[i][j] = edits to turn a[:i] into b[:j]; d[i][0] = i, d[0][j] = j; d[i][j] = d[i-1][j-1] if a[i-1] == b[j-1], else 1 + min(d[i-1][j], d[i][j-1], d[i-1][j-1]). O(|a|·|b|) time; O(min(|a|, |b|)) space keeping two rows.
  • Think aloud. Interviewers are judging how you reason, not only the final code.
  • Be ready for: Reconstruct the actual edits. Only need to know whether the distance is <= k — can you do O(n·k) (banded DP)? Suggest spelling corrections from a dictionary of a million words quickly (BK-tree, tries, n-gram filters). Allow swapping adjacent characters as one edit.

A structure to follow: Clarify, plan, code, test, analyse

  1. Step 1

    Clarify

    Restate the problem, ask about empty input, duplicates and size limits, and agree on one example.

  2. Step 2

    Plan

    Say the simple approach first and its cost, then the better one. Explain why before you type.

  3. Step 3

    Code

    Write small named steps and talk through each. A clear solution beats a clever one.

  4. Step 4

    Test

    Walk your own example through the code, then an edge case. Fix what you find calmly.

  5. Step 5

    Analyse

    State time and space cost in plain words and say what you would change for much larger input.

This is guidance on shape, not a script. Use your own real experience and facts; never invent details to fill a gap. See the STAR guide and answer frameworks.

Common slips

  • Starting to code before agreeing what the answer must be.
  • Going silent when stuck instead of saying what you are trying.
  • Never running your own example through the code.

How to practise it

Solve it once in the editor without hints, then explain your solution aloud in under two minutes.

Browse all questions

Coding problem: Fewest Edits Between Strings. Return the minimum number of single-chara... | How to answer