Designing a Text Editor with Undo and Redo
Difficulty: Intermediate Patterns: Command, Memento, Composite Asked at: Microsoft, Google, Adobe, Notion
Everyone knows what a text editor does, which is why this problem is a good interview: the requirements take ten seconds and the design takes the whole round. Insert at the caret, delete a range, undo, redo. Nobody is going to ask you to clarify what “undo” means. What they are grading is two decisions you make in the first five minutes and cannot walk back later – what the history stores, and what the document is stored in.
The first one is the real question, and the answer is that undo is two mechanisms, not one. Store the operation and its inverse when the inverse is cheap to derive – insert, delete, paste. Store a snapshot of the document when it is not – sort the selected lines, reformat, anything that consumed randomness. Candidates who pick one mechanism and defend it are answering a different question. The rule for choosing is the answer: if the inverse can be computed from the operation plus O(edit size) of saved data, store the command; otherwise store the snapshot. The most common bug in a candidate’s undo falls straight out of that rule: a delete command that records only an offset and a length has no inverse at all, because the characters it removed are gone and nothing in the command says what they were.
The second decision is that text.substring(0, at) + s + text.substring(at) makes every keystroke O(n) in both time and garbage – tens of megabytes of copying per character on a 10 MB file. The document needs a structure that does not touch the far end of the file when you type in the middle, and there is a ladder of them with real trade-offs rather than one right answer.
There is a third thing, smaller, but it separates a working editor from a usable one: Ctrl+Z is a product decision. Nobody expects undo to walk back one character at a time, so consecutive keystrokes have to collapse into one undoable unit, and the rules for when that unit ends – a word boundary, a caret jump, a paste, a pause – are the design. Get them wrong and the editor is technically correct and infuriating.
💡 Invertible = the operation carries enough information to undo itself. insert "world" at 412 is invertible from its own arguments; delete 5 chars at 412 is not, unless it also kept the five characters. Learn more →
💡 Gap buffer = the document held in one array with a movable hole at the caret, so typing writes into the hole instead of shifting the rest of the file. Moving the caret is what costs you.
Functional Requirements
- Editing – insert text at the caret, delete a selected range or the character before the caret, and paste a block.
- Undo and redo – undo reverses the last edit unit, redo re-applies it, and both report failure rather than throwing when there is nothing left.
- Undo units – consecutive typing collapses into one unit, sealed by a word boundary, a caret jump, a paste, or an idle gap.
- Structural edits – sorting the selected lines and reformatting, undoable even though neither inverse can be computed from the operation’s arguments.
- Redo invalidation – a new edit after an undo discards the redo branch.
- Bounded history – the history is capped by a byte budget, not an entry count.
- Bulk undo – walk back many units at once without inverting them one at a time.
Non-Functional Requirements
- O(1) amortised per keystroke at a stationary caret – nothing on the typing path may do work proportional to the document. Moving the caret
dcharacters costs O(d) once, not O(d) per keystroke afterwards. - Undo in O(edit size) – reversing one edit touches the edit, not the document. Only the structural path is O(document), and the user invokes it deliberately.
- Bounded memory – the history is capped in bytes across both stacks. A snapshot and a keystroke differ by orders of magnitude, so “keep the last 100 entries” bounds nothing.
- Deterministic and single-threaded – the clock and the snapshot policy arrive through the constructor, so every grouping rule is an assertion instead of a wait. All edits run on one thread, which is what real editors do.
Discussion
Newest first