Design a Collaborative Document Editor
👥 What is a collaborative document editor? It is a shared document that several people type into at the same time, like Google Docs. Everyone sees everyone else's changes within a moment, nobody's typing is lost, and the document remembers every version. People can highlight a phrase, leave a comment on it, and reply to each other in a thread.
This prompt comes in a few shapes: "Google Docs live editing and concurrency", "document versioning", or "a doc with anchored, nested comments". They are one design. All three are about the same question: what happens to a position in the text when other people edit around it? An edit made on an old version needs its position fixed before it is applied. An old version is the text before some edits. A comment is a range of positions that must follow the text. We build one structure, a log of edits that knows how to move positions, and get all three from it.
This is a hard problem because the obvious code is wrong in a way that only shows up with two users. We will start with that bug, then fix it step by step.
Requirements
"Design the backend of a collaborative document editor like Google Docs. Many users edit the same document at once, it keeps a version history, and users can comment on text and reply to comments."
Before designing anything, find out how edits reach the system and what "at once" really means.
Clarifying Questions
You: Is there a central server that sees every edit, or do clients sync with each other directly?
Interviewer: A central server. Every edit goes through it, and it decides the official order.
You: What does a client send for an edit?
Interviewer: An insert (position and text) or a delete (position and length). Each edit also says which version of the document the user was looking at when they made it. By the time it arrives, other edits may have been committed.
You: What if two edits conflict, say two users type at the same spot, or one deletes a sentence while another types in the middle of it?
Interviewer: Never reject an edit just because someone else got there first. Keep both people's work. If two inserts land on the same spot, the one that reached the server first goes first. A delete removes only the text that user actually saw.
You: What do we need from version history?
Interviewer: Every edit makes a new version. Users can open any old version and read its text. Restoring an old version is a nice-to-have.
You: What is a comment attached to?
Interviewer: A range of text. When people edit around it, the comment must stay on the same words. If someone deletes all of those words, the comment stays in the thread list but has nothing to point at. Comments can have replies, and replies can have replies.
You: Do we handle networking, live cursors of other users, and permissions?
Interviewer: No. Assume edits arrive at the server as method calls. Focus on the document model.
The third answer is the whole problem. "Keep both people's work" rules out the easy options (reject the late edit, or let the last writer win), and "a delete removes only what that user saw" is a precise rule we can code against.
💡 Interview tip: Ask whether there is a central server early. With a server that orders every edit, the standard answer is operational transformation: fix each edit's positions against the edits it missed. Without one (peer-to-peer, long offline editing), the standard answer is a CRDT. Saying which world you are in, and why, is a strong opening.
Final Requirements
- The document starts empty at version 0. Every accepted insert or delete creates the next version: 1, 2, 3, ...
- An edit names its base version: the version the user saw. Positions in the edit refer to the text of that version.
- Before an edit is applied, it is rebased through every edit committed after its base, in commit order. When two inserts land at the same place, the text committed first comes first.
- A delete removes exactly the characters the user saw. Text inserted by others inside the range survives, and characters someone else already deleted are skipped.
- Any version's text can be read.
- A comment is created on a range of characters of some version. It stays attached to those characters as the text changes; text typed inside the range becomes part of the highlighted area. If all its characters are deleted, it has no anchor.
- Comments can be replied to, to any depth. A thread can be read in order with its nesting.
- Invalid requests (a version that does not exist, a position out of range) are rejected with
-1.
Out of scope
- The network, reconnects and the client-side editor (we sketch the client in Extensibility).
- Presence: other users' cursors and selections.
- Permissions, rich text formatting, images.
- Many documents and many servers (a system design question, see /system-design/google-docs).