Figma Document Buffer
Also known as: Text Buffer with Undo/Redo
Figma Document Buffer (Part 1) - insert(position, text) - delete(position, length) - get_text() -> str Rules: - positions are 0-indexed - out-of-bounds positions clip to the nearest valid boundary - insert inserts BEFORE the character currently at position - delete removes up to length characters (clipped by end of document)
Implement a text buffer with insert/delete plus undo/redo. The core trap is that undoing deletes/replaces requires remembering exactly what text was removed.
Examples
You write
DocumentBuffer: insert(position: int, text: str), delete(position: int, length: int), get_text()
Example 1
- Input
- insert(0,"hello"), getText()→"hello", insert(5," world"), getText()→"hello world", delete(5,1), getText()→"helloworld"
- returns
- ["hello","hello world","helloworld"]
Example 2
- Input
- Clipping: insert beyond end appends; delete beyond end does nothing
- returns
- ["hi","hi!","hi!"]
The pattern behind it
Design
I reach for it when
- The statement describes a class with methods, not one function.
- Each method needs to be fast, and the structure decides how fast.
Say this before you type. Say each method, the cost you want from it, and the structure that earns that cost.
All 27 Design problems
Meet Coco
Coco is an AI study partner, not a person. She listens while you explain your code, asks what a line does and never hands you the fix. Sign in free to type with Coco, up to a daily limit. The editor and Run stay open without an account. Anyone can open the editor and run their code.
Each locked part opens in the editor once the part before it passes.