Caption Editor With Cursor Movement, Backspace and Undo
Last updated: November 29, 2026
Quick Overview
Implement a text editor for post captions supporting type(text) at the cursor, backspace(k), move(k) left or right, undo() of the last operation, and reading the text and cursor position. Each operation should cost time proportional to the characters it touches, not the caption length.
Meta
November 29, 20260
0
0 solved
Implement a text editor for post captions supporting type(text) at the cursor, backspace(k), move(k) left or right, undo() of the last operation, and reading the text and cursor position. Each operation should cost time proportional to the characters it touches, not the caption length.
Composers in Facebook, Instagram and Threads all need fast cursor edits with undo. A Meta onsite uses this to test a two-stack gap buffer and a command history of inverse operations, plus your ability to reason about many edge cases.
What the Interviewer Expects
- Represent the text as two stacks around the cursor
- Make type, backspace and move proportional to k
- Record the inverse of each operation for undo
- Store only what actually happened (clamped deletes and moves)
- Walk through an undo sequence to prove correctness
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- How would you add redo?
- How would you group consecutive keystrokes into one undo step?
- How would you support selections and replacing a selected range?
- How would collaborative editing change the design?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
type(s)inserts at the cursor, cursor ends after the inserted text.backspace(k)deletes up to k characters left of the cursor and returns how many were deleted.move(k)moves the cursor, ...
High-Level Design
Keep left (characters before the cursor) and right (characters after it, reversed so the nearest is at the end). Typing appends to left; backspace pops from left; moving shifts characters betw...