Module 10 · DSA · Built from the toolbox · with code: Python · about 19 min to read
Continue where you left off ×
Build a diff: the table
Recursion and Dynamic Programming · Open board
Is the board this build leans on. It says dynamic programming is “just recursion that remembers”: answer each small question once, write it down, and build the big answer from answers already written. This lesson writes those answers into a grid for two whole files, turns the grid into a real diff, and then measures how much writing everything down costs.
Before you start. Dynamic Programming, Worked (filling the table) filled a grid for two protein chains: each cell answered “how much do these two beginnings share, in order?”, and the answer for the whole chains waited in the bottom-right cell. Clinic #22: Longest Common Subsequence solved the same grid as an interview problem, and Git's diff (Myers' algorithm), the case study , later in this track, tells git’s side. Here you build the tool yourself, one file of Python, and watch it run out of room.
The idea in 30 seconds
A diff finds the longest common subsequence of two files: the most lines that appear in both, in the same order. Every other line was removed or added.
The table has one cell per pair of lines, and each cell is filled from the cell up-left (if the two lines match) or the bigger of the cells above and to the left (if not).
Walking back from the bottom-right cell recovers the edit script: keep, remove and add instructions that turn the old file into the new one with the fewest edits.
diff -u prints only the changes, grouped into hunks with three unchanged context lines on each side and an @@ -old +new @@ address.
The table’s memory is quadratic: twice the lines, four times the cells. Measured in Python, 16,000 lines took 2.2 GB, and 50,000 lines would need more than a 16 GB laptop has.
Read the story
Lesson 93 of 96 in DSA
The rest of this lesson is part of a plan.
A plan opens every lesson, board, video, lab and paper on chalk. Yearly is $39/year .
See plans Already a member? Sign in
Or start free : a free account opens lessons 2 and 3 of every track.
In this lesson June 1975: a three-page paper about running out of memory Two packing lists, and the lines they share The table: one cell for every pair of lines Walking back: the edit script Hunks: what diff -u actually prints The wall: twice the lines, four times the memory The table and the walk back The packing list Against diff -u on 10,000 lines The memory, measured Running out, on purpose What Hirschberg’s paper did, and what it did not In an interview Terms it explains: Longest Common Subsequence, edit script, context lines, hunk, quadratic, Edit Distance, base case, dynamic programming, greedy, knapsack / edit distance / LCS, memoization, optimal substructure, overlapping subproblems, recurrence, recursion, recursion tree, rolling array, state, tabulation, time, Viterbi.
Review DSA
Mark as read Saved in this browser, and to your account when you sign in.