{}DSA Atlas

Design a Collaborative Editor (Google Docs / Figma)

Many people editing the same document in real time — why naive last-write-wins fails, operational transformation with a central server, CRDTs for decentralised merging, the document session service and routing all editors of a doc to one owner, cursors and presence, persistence with operation logs and snapshots, offline editing, comments, permissions and version history.

Advanced5 interview questions

1. Requirements

Functional

  • Multiple users edit a rich-text document simultaneously; see each other's edits in real time (< 200 ms).
  • Live cursors/selections and presence avatars.
  • Comments and suggestions; version history; offline editing that merges later.
  • Sharing with view/comment/edit permissions.

Non-functional

  • Convergence: all users end with the same document; no lost edits.
  • Scale: 100M documents active monthly; typical doc has 1–10 concurrent editors, some have 100+ (viewers in thousands).
  • Durable: acknowledged edits never lost.

2. The core problem

"abcdef" Alice: insert(5, "X") → "abcdeXf" Bob: delete(1) → "acdef" Alice applies Bob's delete(1)"acdeXf" ✓ Bob applies insert(5,"X") naively"acdefX" ✗ diverged OT: transform insert(5) against delete(1) → insert(4) · CRDT: insert after char-ID of "e"
Concurrent index-based operations diverge unless transformed or made ID-based.

3. Operational transformation

Python
def transform_insert_insert(a, b, a_wins_ties):
    """Adjust insert a so it applies after insert b has been applied."""
    if a.pos < b.pos or (a.pos == b.pos and a_wins_ties):
        return a
    return Insert(a.pos + len(b.text), a.text)

def transform_insert_delete(a, d):
    """Adjust insert a so it applies after delete d (removes d.length chars at d.pos)."""
    if a.pos <= d.pos:
        return a
    if a.pos >= d.pos + d.length:
        return Insert(a.pos - d.length, a.text)
    return Insert(d.pos, a.text)            # insertion point was inside the deleted range

Server loop (per document): receive (op, base_rev, client_op_id) → transform op against ops base_rev+1 … head → apply → head += 1 → persist → ack the sender with the new revision → broadcast to others.

Client state machine: at most one op "in flight" awaiting ack + a buffer of local ops; incoming server ops are transformed against in-flight and buffered ops (the classic Jupiter/Google Wave algorithm).

4. CRDT alternative

Text
Each character has an ID (client, counter) and a left origin:
  insert "X" with id (alice, 17) after id (bob, 4)
  delete marks id (carol, 9) as tombstone
Replicas integrate ops by IDs → same order everywhere, no transformation, no central sequencer.
Libraries: Yjs, Automerge, Loro, Diamond Types.
OTCRDT
Server rolerequired sequenceroptional relay/storage
Offline / P2Plimited (long offline = big transforms)natural
Metadata overheadlowper-character IDs, tombstones (compressed)
Correctnesstransform functions are trickyalgebraic guarantees
Used byGoogle Docs and other server-centric editorsYjs/Automerge-based editors, local-first apps; Figma uses CRDT-inspired per-property merging

5. High-level design

Alice Bob Carol WS gatewaysauth · route bydoc ID doc session ownerdoc 42 in memorysequence · transformbroadcast · presence(owner via consistent hash) op log (append, by doc) snapshots (object store) doc metadata + ACLs comments service search indexer (async)
One in-memory owner per active document; durable op log and snapshots behind it.

6. Deep dives

Presence and cursors

Ephemeral — never persisted to the op log. Clients send cursor/selection updates (throttled ~10/s) to the owner, which broadcasts them; cursors are expressed relative to character IDs or transformed like ops so they stay attached to the right text.

Large audiences

Hundreds of editors: batch broadcasts (every 50 ms), compress, and limit cursor broadcasts to visible ones. Thousands of viewers: put viewers on a read-only fan-out tier (pub/sub) fed by the owner rather than direct connections to it.

Offline editing

Client stores ops locally (IndexedDB). On reconnect it sends them with the last known revision; with OT the server transforms a potentially long sequence; with CRDTs merge is automatic. Very long divergence may need a user-facing "review changes" UI.

Permissions and comments

Permission checks at connection time and on each op type (comment-only users can only send comment ops). Comments anchor to ranges by character IDs (or are transformed), stored in a separate service.

Version history

Group ops into versions by time/session ("Edited by Alice, 3:42 PM"); restore = new ops that transform the current doc to the old state (history remains append-only).

7. Trade-offs

DecisionChoice
AlgorithmOT with central server (mature for text) vs CRDT (offline/P2P, simpler server)
Ownershipconsistent hashing of doc → session owner with lease
Persistenceop log + periodic snapshots
TransportWebSocket; ops batched
Presenceephemeral, throttled, best effort

Interview questions

0 / 5 marked confident

Answer out loud first, then open the model answer. Mark a question confident only when you could explain it to an interviewer without notes.