Text Editor With a Cursor Backed by a Doubly Linked List
Last updated: November 29, 2026
Quick Overview
Build a text editor that keeps a cursor and supports addText(s), deleteText(k) (backspace up to k characters, return how many were deleted), and cursorLeft(k) / cursorRight(k), each returning the last up to 10 characters to the left of the cursor.
0
0
0 solved
Build a text editor that keeps a cursor and supports addText(s), deleteText(k) (backspace up to k characters, return how many were deleted), and cursorLeft(k) / cursorRight(k), each returning the last up to 10 characters to the left of the cursor.
A Google onsite question for front end and generalist roles, inspired by editing in Docs and Keep. The interviewer wants operations proportional to k or len(s), not the document size, and checks sentinel handling at both ends.
What the Interviewer Expects
- Represent text as a doubly linked list of characters with head and tail sentinels
- Keep the cursor as the node immediately to its left
- Make each operation O(k) or O(len(s)), independent of document length
- Return the 10 characters left of the cursor by walking back at most 10 nodes
- Compare with the two-stack (gap buffer) approach
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
- Add undo and redo.
- Support selecting a range and replacing it.
- Many collaborators edit at once; what breaks?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
addText(s): insert s at the cursor; cursor ends after it.deleteText(k): delete up to k characters left of the cursor; return count deleted.cursorLeft(k),cursorRight(k): move up to k p...
High-Level Design
An array would make every insert O(n) because characters to the right must shift. A doubly linked list with head and tail sentinels lets us insert and delete next to a known node in O(1). The cursor i...