Text Editor Buffer With Cursor Moves and Multi-Level Undo

Last updated: November 29, 2026

Quick Overview

Implement a text buffer for an editor panel that supports moving the cursor, inserting text at the cursor, deleting characters to the left, and unlimited undo and redo that restore both text and cursor position.

OpenAI
Coding & Algorithms
Software Engineer
OpenAI
November 29, 2026
Software Engineer
Technical Screen
Coding & Algorithms
Medium

0

0

0 solved


Implement a text buffer for an editor panel that supports moving the cursor, inserting text at the cursor, deleting characters to the left, and unlimited undo and redo that restore both text and cursor position.

Canvas and code editing surfaces in ChatGPT need reliable undo when both the user and the model edit the same document. OpenAI interviewers use this to see whether you store inverse operations instead of full snapshots, keep redo semantics right (new edits clear redo), and restore the cursor along with the text.

What the Interviewer Expects
  • Store operations (with enough data to invert them) rather than whole-text snapshots
  • Clear the redo stack on any new edit
  • Restore cursor position on undo and redo
  • Clamp cursor moves and deletions at the boundaries
  • Explain the cost of string slicing and when to switch to a rope or gap buffer
Key Topics to Cover
stacks
undo and redo
command pattern
data structure design
text buffers
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • Group consecutive single-character inserts into one undo step, like typing a word.
  • The model applies an edit while the user is typing: how do you keep undo history meaningful for each actor?
  • Swap the string for a gap buffer: what changes in the complexity?
  • Persist the history so undo survives a page reload.
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements
  • move(pos): clamp to [0, len(text)].
  • insert(s): insert at the cursor; cursor ends after the inserted text.
  • backspace(n): delete up to n characters left of the cursor.
  • undo() / `redo...
High-Level Design

Each edit is a record (kind, position, text, cursor_before), which is enough to both apply and invert it. Applying pushes the record on the undo stack. Undo pops it, performs the inverse (remove ins...


Submit Your Answer
Markdown supported