Text editor with undo and redo
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in OpenAI's technical screen (coding) and at Notion, last asked Mar 2026. It tests undo, stacks and text editing.
Build a text editor that edits only at the end of the document. It appends text, deletes characters from the end, and supports undo and redo. Model every edit as an operation object that knows how to apply itself and how to produce its inverse.
Interface
Unless you agree on a different interface with the interviewer, assume:
-
insert(text: str) -> None— appendstext. -
delete(k: int) -> None— removesmin(k, len(content))characters from the end. -
undo() -> None— reverts the most recent effective edit that has not been undone; does nothing if there is none. -
redo() -> None— re-applies the most recently undone edit; does nothing if there is none. get_content() -> str- An
Operationinterface withapply(doc) -> docandinverse() -> Operation. An insert of n characters inverts to a delete of n; a delete inverts to an insert of the exact text it removed. - A new effective edit clears the redo history. An edit that changes nothing (empty insert, delete on an empty document,
delete(0)) is not recorded and does not clear redo.
Example
insert("abcd") -> "abcd"
delete(1) -> "abc"
delete(1) -> "ab"
undo() -> "abc"
undo() -> "abcd"
redo() -> "abc"
delete(0) -> "abc" # no effect, redo is kept
redo() -> "ab"
delete(10) -> "" # removes only the 2 characters left
undo() -> "ab"
Constraints
- Up to 10^4 operations; inserted text up to 10^5 characters in total.
Clarifying questions
- If
kis larger than the document, delete everything or reject the call? - Does an edit that changes nothing count as an operation for undo, and does it clear redo?
- Should consecutive inserts merge into one undo step, as many editors do?
- Is there a limit on how much history to keep?
What a strong answer covers
- Two stacks of operations, undo and redo, and the exact rule for when redo is cleared.
- A delete that records the removed text when it is applied, since deleting loses information.
- The cost of each call: with a list of characters (or a list of chunks), insert and delete are O(length of the change), not O(document); an immutable string costs O(document) per edit.
- Tests: simple undo and redo, several deletes in a row, a delete longer than the document, undo and redo on empty stacks, and a no-op edit between undo and redo.
Follow-ups
- Add a cursor: insert and delete at any position, and say what each operation must now store.
- Group edits typed within one second into a single undo step.
- Cap the history at N operations, and say what memory a long session costs.
Where it's asked
- OpenAI · Technical screen: coding · Last asked Mar 202660 min · One problem in three to five growing parts filling the hour, usually in CoderPad; ML roles get a Colab notebook with autocomplete off · No AI assistants or autocomplete; documentation is fine
- Notion · Technical phone screen · Last asked Mar 202660 min · Live coding in CoderPad
What it tests
undo · stacks · text editing
Sign in to see the worked answer
The full answer, chapter by chapter, with what interviewers expect at each level. New accounts get 5 credits: each one unlocks a question for good.
What the labs asked this week
A weekly email of what OpenAI, Anthropic and the other top AI labs asked engineers, round by round.
Sign in to submit your code; every attempt is kept here.

