Implement Undo and Redo for a Text Editor With Commands
Last updated: November 29, 2026
Quick Overview
Implement undo and redo for a simple text editor that supports insert and delete. Each user edit should be undoable, consecutive typing should collapse into one undo step, and a new edit after undo should clear the redo history.
Microsoft
November 29, 20260
0
0 solved
Implement undo and redo for a simple text editor that supports insert and delete. Each user edit should be undoable, consecutive typing should collapse into one undo step, and a new edit after undo should clear the redo history.
A natural fit for Microsoft given Word, Visual Studio Code and the Office apps. The interviewer checks use of the Command pattern, the two-stack model, capturing enough state to invert a delete, and product judgment about grouping keystrokes into sensible undo units.
What the Interviewer Expects
- Encapsulates each edit as a command with do and undo
- Uses an undo stack and redo stack with correct transitions
- Captures deleted text so delete can be undone
- Clears redo history when a new edit arrives
- Coalesces consecutive typing into a single undo step
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 undo work in a co-authoring session where others edit too?
- How would you cap memory for very long editing sessions?
- How would you group a find-and-replace-all into one undo step?
- Would you store inverse commands or document snapshots, and why?
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
execute(Insert(pos, s))andexecute(Delete(pos, n))modify the document.undo()reverts the last undo unit;redo()reapplies it.- Typing characters one by one at consecutive positions is ...
High-Level Design
Command pattern: each edit knows how to do and undo itself. Insert can undo from its own fields. Delete must capture the removed text at execution time, since the inverse is not deriva...