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.

Google
Coding & Algorithms
Frontend Engineer
Google
November 29, 2026
Frontend Engineer
Onsite
Coding & Algorithms
Hard

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
doubly linked list
sentinel nodes
cursor management
gap buffer
data structure design
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
  • 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 Problems
Sample 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...


Submit Your Answer
Markdown supported