Design a Text Editor
📝 What is a text editor? It is the part of any app that holds the text you are typing: a notes app, a code editor, the box you write a comment in. There is a blinking cursor, keys that insert and delete at the cursor, arrow keys that move it, and an undo button that takes back your last change. Redo brings it back again.
This problem looks small, and that is the trap. Typing at a cursor is easy to code with a plain string, but the interviewer is really testing two things: whether you pick a data structure that makes the common operations cheap, and whether your undo/redo is a clean design instead of a pile of special cases. Both have a classic answer, and the fun is in showing why the obvious version falls short first.
Requirements
"Design a text editor. Users can type, delete, move the cursor, and undo or redo their changes."
The prompt hides a lot of choices. What exactly is a "change"? Does moving the cursor count? Where does the cursor go after an undo? Ask before you write a line of code.
Clarifying Questions
You: Is there one cursor, or can the user select a range of text?
Interviewer: One cursor, no selection for now. We might add selection later.
You: How does deleting work? Backspace, the Delete key, or both?
Interviewer: Backspace only: delete up to k characters to the left of the cursor. Return how many were actually deleted, since there may be fewer than k.
You: Do cursor moves count as changes? If I move the cursor and press undo, does the cursor jump back?
Interviewer: No. Undo only reverts edits to the text. Cursor moves are not recorded.
You: How big is one undo step? A single character, a word, or a whole addText call?
Interviewer: One call is one step. If the user adds "hello" in one call, one undo removes all of it.
You: If I undo twice and then type something new, can I still redo?
Interviewer: No. A new edit throws away the redo history, like every editor you have used.
You: What does the UI need back when the cursor moves?
Interviewer: Return the last 10 characters to the left of the cursor. That is what the status bar shows.
You: How large are the documents?
Interviewer: Normal documents, a few thousand characters. But tell me how your design would cope with a huge file.
The third and fourth answers matter most. Because cursor moves are not undo steps, the cursor can be anywhere when the user presses undo. An undo that says "delete the 5 characters left of the cursor" would delete the wrong text. Each recorded edit has to remember where in the text it happened, not where the cursor is now.
💡 Interview tip: Always ask what counts as one undo step. "Each call", "each word" and "each keystroke" lead to different designs, and interviewers love to come back to it as a follow-up ("can you group typing into words?").
Final Requirements
- The editor holds one piece of text and one cursor. It starts empty with the cursor at position 0.
addText(text)inserts the text at the cursor. The cursor ends up after the inserted text.deleteText(k)deletes up tokcharacters to the left of the cursor and returns how many it deleted.cursorLeft(k)andcursorRight(k)move the cursor up tokpositions, stopping at the ends of the text, and return the last 10 characters left of the cursor.undo()reverts the most recent edit that has not been undone.redo()re-applies the most recently undone edit. Both returnfalseif there is nothing to do.- An edit is an
addTextcall or adeleteTextcall that deleted something. Cursor moves are not edits. - Undo and redo act at the position where the edit happened, wherever the cursor is. After undoing an insert, the cursor sits where the text started; after undoing a delete, it sits right after the restored text.
- A new edit clears the redo history.
Out of scope
- Selection, copy and paste (we come back to it in Extensibility).
- Formatting such as bold and italic.
- Files, saving and loading.
- Several users editing at once. That is a different problem: see /low-level-design/collaborative-editor.