Build an Undo Manager That Coalesces Typing in a Text Editor
Last updated: November 29, 2026
Quick Overview
Implement undo and redo for a text editor using commands, where consecutive keystrokes typed close together merge into one undo step, like typing in Pages or Notes.
Apple
November 29, 20260
0
0 solved
Implement undo and redo for a text editor using commands, where consecutive keystrokes typed close together merge into one undo step, like typing in Pages or Notes.
Apple apps expose undo through UndoManager and shake-to-undo, and users expect one undo to remove a word, not a letter. The interviewer checks the command pattern, the two-stack model, and sensible coalescing rules.
What the Interviewer Expects
- Uses the command pattern with apply and revert on each edit
- Keeps undo and redo stacks and clears redo on a new edit
- Coalesces adjacent inserts within a time window and breaks groups at whitespace
- Delete commands capture removed text at apply time so they can be reverted
- Bounds history size
Key Topics to Cover
How to Approach This
- Apply SOLID principles. Single Responsibility makes code testable, Open/Closed makes it extensible.
- Choose data structures based on access patterns, not familiarity.
- Prefer immutable data and message passing over shared mutable state for concurrency.
- Design APIs with RESTful conventions, versioning, meaningful errors, and pagination from day one.
Possible Follow-up Questions
- How would you group a whole paste or find-and-replace into one undo step?
- How does undo work in a collaborative document where others are editing too?
- How would you persist undo history across app launches?
- How does Foundation's UndoManager grouping map onto this design?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice System Design ProblemsSample Answer
Requirements
perform(cmd)applies an edit and records it for undo.undo()andredo()move between states; a new edit clears redo.- Consecutive single-character inserts at the cursor within 1 second coal...
High-Level Design
Each command implements apply(doc), revert(doc), and try_merge(next). The manager asks the top undo command to absorb the new one before pushing. InsertText merges only if the new text starts ...