Build a Notepad Text Buffer With Cursor Moves and Undo

Last updated: November 29, 2026

Quick Overview

Implement a text buffer that supports inserting text at the cursor, deleting k characters to the left of it, moving the cursor left or right by k (returning the last 10 characters before the cursor), and undoing the most recent insert or delete.

Microsoft
Coding & Algorithms
Software Engineer
Microsoft
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Medium

0

0

0 solved


Implement a text buffer that supports inserting text at the cursor, deleting k characters to the left of it, moving the cursor left or right by k (returning the last 10 characters before the cursor), and undoing the most recent insert or delete.

A Microsoft onsite question modeled on the editing core of Notepad or a VS Code text model. The interviewer is looking for a gap-buffer style design with two stacks so that edits at the cursor are cheap, plus a correct undo that works even after the cursor has moved.

What the Interviewer Expects
  • Use two stacks (left and right of the cursor) or a gap buffer
  • Make insert and delete proportional to the text changed, not the document size
  • Record enough in each undo entry to restore position and content
  • Move the cursor back to the edit position before undoing
  • Bound cursor moves at document edges
Key Topics to Cover
stacks
gap buffer
undo
data structure design
simulation
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
  • How would you add redo?
  • How would a rope or piece table compare for very large files?
  • How would you group consecutive keystrokes into one undo step?
  • How would you support multiple cursors?
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

Operations: insert(text) at the cursor, delete(k) removes up to k characters left of the cursor and returns how many were removed, move_left(k) and move_right(k) clamp at the ends and return u...

High-Level Design

Split the text at the cursor into two stacks: left holds characters before the cursor in order, and right holds characters after it in reverse, so the character next to the cursor is on top of bot...


Submit Your Answer
Markdown supported