Notepad System
Text editor with undo and redo
Design a small text editor: notes with insert and delete, unlimited undo and redo, tags and search. The undo stack is the heart of the design.
Overview
A notepad keeps a set of notes. Each note has a title, a body of text and some tags. The user inserts text, deletes text, and can take any edit back with undo, then put it back with redo. There is also a search box.
Interviewers ask this problem because undo is a clean, everyday case for the Command pattern: every edit becomes a small object that knows how to do itself and how to reverse itself. Two stacks of those objects give unlimited undo and redo in a few lines. The rest of the problem, notes, tags and search, tests whether you can keep each class to one job.
A strong answer names the two stacks in the first minute, explains why a new edit must empty the redo stack, and writes commands that store only what they need to reverse themselves, not a copy of the whole note.
Scoping the problem
Ask these questions before you write code. Each answer changes the design, and each comes with the answer interviewers usually give.
- Is undo per note, or one history for the whole app? This decides where the history object lives. The usual answer is per note, like most editors.
- Is there a limit on undo depth? A bounded history drops the oldest command. Assume unlimited in the interview; a cap is a one-line change.
- Which edits are undoable: text only, or title and tags too? Every undoable change must become a command. Do text in the core and leave the rest as a follow-up.
- One user or many at once? Concurrent editing is a different problem, covered in the follow-ups. Assume one user, so no locks are needed.
- How large can a note be? This decides whether a plain string is enough. Notes are small, so a string works.
- What does search match? Title, body or tags, and whether case matters. The usual answer is a case-insensitive match on title and body.
Functional requirements
- Create notes with a title; read, rename and delete them
- Insert text at a position and delete a range of text in a note
- Undo and redo any number of text edits, per note
- A new edit after an undo discards the redo history
- Add and remove tags on a note; list notes by tag
- Search notes by a case-insensitive match on title or content
Non-functional requirements
- Undo and redo are O(1) stack operations plus one string splice
- A history entry holds the delta only, never a copy of the whole note
- Invalid positions and unknown note ids raise errors instead of silently doing nothing
- Single user; the classes are not shared between threads
Interview questions
- Why store commands instead of a snapshot of the note before each edit?
- Typing one character at a time would make undo remove one letter per step. How do you group edits?
- What should happen to undo if the note is renamed or a tag is added?
- Two users edit the same note at once. What changes?
- A 100 MB log file is opened in the editor. What breaks?
- What bugs do candidates write here, and how do you avoid them?
Sign up free to read the full topic
More Low-Level Design topics
Start free on Cariara · Coding problems · System design studio