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
Software Engineering Fundamentals
Software Engineer
Apple
November 29, 2026
Software Engineer
Technical Screen
Software Engineering Fundamentals
Easy

0

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
command pattern
undo/redo
coalescing
stacks
text editing
How to Approach This
  1. Apply SOLID principles. Single Responsibility makes code testable, Open/Closed makes it extensible.
  2. Choose data structures based on access patterns, not familiarity.
  3. Prefer immutable data and message passing over shared mutable state for concurrency.
  4. 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 Problems
Sample Answer
Requirements
  • perform(cmd) applies an edit and records it for undo.
  • undo() and redo() 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 ...


Submit Your Answer
Markdown supported