TRT
Sign in
hardAdvanceddesign

How would you compute edit distance between two very large strings?

Start from the O(m × n) table, reduce memory to two rows, recover the edits in linear space, and speed up the common case.

TRT practice prompt — not a verified question from a named employer.

The Runtime Theory Team1 min read
Learn it firstRebuilding the answer and saving memoryPro — members onlyA DP table holds the value of the best solution, not the solution itself. Walking back through the table recovers the choices. When only the value is needed, keeping one or two rows cuts memory from O(n × m) to O(m).

A strong answer

The standard table, where dp[i][j] is the distance between the first i characters of one string and the first j of the other, costs O(m × n) time and space. For two 100,000-character strings that is ten billion cells, around 40 GB.

If only the distance is needed, each row depends only on the previous one, so two rows suffice: O(n) memory, about 800 KB here, with the same time. If the edits themselves are needed, Hirschberg's algorithm recovers them in O(m + n) memory: run the two-row DP forward over the first half of one string and backward over the second half, combine the two middle rows to find where an optimal path crosses the middle, then recurse on the two halves. It roughly doubles the time but keeps memory linear.

Time is often the bigger problem. If the strings are similar, or I only need to know whether the distance is at most k, I compute only a band of width 2k + 1 around the diagonal, since any cell farther away has distance above k: O(k × n). Doubling k until the answer fits gives O(d × n) for a true distance d. Myers-style algorithms exploit the same structure. For huge inputs on modern hardware, the anti-diagonals of the table are independent and can be computed in parallel or with vector instructions, and bit-parallel algorithms pack many cells into machine words.

What the interviewer is listening for

  • Quantifying the table's size and recognizing it won't fit.
  • Two-row space reduction, and why it loses the edits.
  • Hirschberg for linear-space reconstruction.
  • Banded or diagonal approaches when the distance is small.

Common mistakes

  • Proposing the full table without checking its size.
  • Assuming two rows are enough to print the edits.
  • Ignoring that similar inputs allow much faster algorithms.

This answer walks

Practice follow-ups

  1. 01How does Hirschberg's algorithm find the split point?
  2. 02If you only need to know whether the distance is at most k, how fast can you be?
  3. 03How would you parallelize the table computation?
  4. 04Which applications need the edits rather than just the distance?

Not started

Sign in to save your learning progress.

Sign in to save