← Questions

Text editor with undo and redo

0
NotionOpenAICodingLast asked Mar 2026

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 — appends text.
  • delete(k: int) -> None — removes min(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 Operation interface with apply(doc) -> doc and inverse() -> 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 k is 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

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding