The Design a Text Editor interview question involves building a simulation of a basic text editor. You need to support inserting text at a cursor, deleting text before the cursor, and moving the cursor left or right. When moving, you also need to return a snippet of the text surrounding the cursor. This Design a Text Editor coding problem is all about efficient string and pointer manipulation.
Companies like Uber and Meta ask this to evaluate your ability to handle Dynamic Data Streams. It tests your knowledge of Linked List interview patterns or Stack interview patterns for representing a "gap" or a "cursor." It evaluates how you handle large strings that undergo frequent modifications at an arbitrary midpoint.
The most efficient approach is the Two-Stack Pattern (or a Doubly-Linked List with a cursor pointer).
Initial: | (Left Stack: [], Right Stack: [])
addText("hello"): hello| (Left: [h,e,l,l,o], Right: [])cursorLeft(2): hel|lo (Pop 'o', 'l' from Left, push to Right. Left: [h,e,l], Right: [o,l])deleteText(1): he|lo (Pop 'l' from Left. Left: [h,e], Right: [o,l])cursorRight(1): hel|o (Pop 'l' from Right, push to Left. Left: [h,e,l], Right: [o])substring() for every operation, which is and extremely slow for large texts.Whenever you have a "Cursor" that moves back and forth in a sequence, think of the Two-Stack approach. It’s also the secret to solving the "Undo/Redo" logic in many software design questions.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Max Stack | Hard | Solve | |
| Design Browser History | Medium | Solve | |
| Removing Stars From a String | Medium | Solve | |
| Remove All Occurrences of a Substring | Medium | Solve | |
| Resulting String After Adjacent Removals | Medium | Solve |