Design a collaborative document editor
Several people edit the same document at the same time and see each other's changes live. Design it.
What they are actually testing
How you handle concurrent edits to the same data, and whether you engage with alternatives properly. Expect the interviewer to suggest a different approach partway through and watch whether you evaluate it or defend by reflex.
Show model answer
The hard part is concurrent edits, not transport. Sending changes over a socket is straightforward. Deciding what the document *is* when two people edit the same position simultaneously is the problem.
Why the obvious options fail. Storing the whole document and taking the last save silently destroys the other person's work. Sending positional operations naively fails because position 5 means different things depending on what has already been applied. Apply the same two operations in different orders on two machines and the documents diverge permanently.
Two real families. Operational Transformation transforms an incoming operation against operations already applied locally, so the result converges. It works, Google Docs is publicly documented as using it, and it is hard to get right: the transformation functions are subtle and usually need a central server to impose an order. CRDTs design the data type so that concurrent changes merge to the same result regardless of order. Each character gets a unique identifier rather than an index, so "insert after character X" is unambiguous no matter what else happened. CRDTs are simpler to reason about and to distribute. They cost metadata per character, which means memory overhead, and they need care with tombstones for deleted content.
I would take a CRDT for a new system, mainly because it does not require a central serialisation point and offline editing falls out naturally. But I would say plainly that OT is a legitimate choice with a longer production track record, and if the interviewer prefers it I would want to hear why. This is an open engineering disagreement, not a settled question.
Transport. WebSockets, because edits flow both directions constantly. Server holds the authoritative document and broadcasts changes to everyone in the session. Clients apply their own edits optimistically so typing feels instant, then reconcile.
The boring parts that matter. Presence and cursors are ephemeral and can be lossy, so do not persist them. A new joiner loads a snapshot plus operations since, so you periodically compact operations into snapshots or joining a long-lived document becomes slow. Persistence is the operation log, with snapshots as an optimisation; that also gives version history for free.
Numbers. Most documents have one to five editors at a time; a popular one might have a hundred viewers and a dozen typists. A fast typist produces about five to ten keystrokes a second, so even a busy document is a few hundred operations a second. The scale problem is the number of documents open at once, not the traffic inside one, which means the design should keep each document on one server and spread documents across many.
One server owns each open document. Route every connection for a document to the same session server, for example by consistent hashing on the document id. That server holds the live state in memory, orders incoming operations, and broadcasts them. Different documents land on different servers, so capacity grows by adding servers. If a server dies, its documents are reopened elsewhere from the last snapshot plus the operation log, and clients reconnect.
Data model. document(id, owner, latest_snapshot_version), snapshot(doc_id, version, blob) and an append-only operation(doc_id, seq, client_id, client_seq, payload). The pair (client_id, client_seq) is unique, so an operation resent after a reconnect is recognised and applied once.
Messages. Client to server: "here are my operations since sequence N". Server to clients: operations with their assigned sequence numbers, plus presence updates. A joining client asks for the latest snapshot and every operation after it.
Permissions. Checked when the socket opens and again when access changes: if an editor is downgraded to viewer, the server must stop accepting their operations straight away, not at their next reconnect. Viewers receive the stream but cannot send operations.
Failure modes. A reconnecting client sends operations it made while offline, which must merge rather than overwrite. The CRDT choice makes this ordinary rather than special. A slow client must not hold up others, so per-connection buffers are bounded and a client that falls too far behind is dropped and resyncs from a snapshot.
All 5 hints
- Hint 1 — What is the hard part?: It is not the network. Two people type at the same position at the same moment. What has to be true about the result?
- Hint 2 — Why not just last-write-wins?: If you store the whole document and take the last save, what happens to the other person's work?
- Hint 3 — What if you sent changes instead?: Rather than the document, send "insert X at position 5". What goes wrong when two of those arrive in different orders on different machines?
- Hint 4 — Two known families of solution: One transforms incoming operations against ones already applied. The other designs the data type so merging is order-independent. What does each cost?
- Hint 5 — Do not forget the boring parts: Presence, cursors, offline editing, and what a new joiner loads. Which of these is actually hard?
The follow-up they will ask next
"I think operational transformation is the better choice here. Convince me otherwise — or agree."
What a strong answer covers (the rubric)
- Identified concurrent edits as the hard part, not the transport
- Explained why last-write-wins and naive positional ops both fail
- Named both OT and CRDTs and described the actual difference
- Made a choice and named what it costs
- Engaged with the alternative rather than dismissing it
- Optimistic local application so typing feels instant
- Snapshot plus operation log, with compaction for new joiners
- Presence and cursors treated as ephemeral
- Handled reconnect-after-offline and slow clients
- Noticed the scale problem is open documents, not traffic per document
- One session server per open document, documents spread across servers
- Operation log with unique client sequence numbers, so resends apply once
- Permission changes take effect on the live connection
This question is written in the style of Microsoft’s interview round. This site is not affiliated with or endorsed by Microsoft.