Design a Document Collaboration System (Google Docs)

Hard
Real World SystemsOperational TransformConcurrencyCollaborationVersioningPermissions

Problem Statement

Design a simplified real-time document collaboration system like Google Docs where multiple users can simultaneously edit a document, with conflict resolution, version history, and access control.

Requirements

Functional Requirements

  • Create, read, update, delete documents
  • Multiple users can edit the same document simultaneously
  • Real-time sync: changes by one user visible to others immediately
  • Conflict resolution: last-write-wins or simplified operational transform
  • Document versioning: save checkpoints; view and restore previous versions
  • Permissions: OWNER, EDITOR, VIEWER — granular access control
  • Sharing: share via link (view-only or edit) or invite specific users
  • Comments: attach comments to text ranges
  • Offline editing: queue edits locally and sync when online

Non-Functional Requirements

  • Thread-safe concurrent edits
  • Event-driven real-time sync using Observer pattern
  • Singleton DocumentService

Key Entities

  • Document: id, title, content (StringBuilder), owner, version, collaborators, comments
  • Edit: documentId, userId, operation (INSERT/DELETE/REPLACE), position, text, timestamp
  • Version: id, documentId, content snapshot, savedAt, savedBy
  • Permission: userId, documentId, role (OWNER/EDITOR/VIEWER)
  • Comment: id, documentId, textRange, author, body, resolved
  • DocumentService: Singleton; manages docs, edits, versions, permissions

Constraints

  • VIEWER cannot make edits
  • Version history retains last 50 versions
  • Concurrent edits from same document serialized via document-level lock
  • Comments can only be added by EDITORs and OWNERs

!Constraints

- VIEWER permission is read-only - Concurrent edits serialized per document (document-level lock) - Version history: last 50 checkpoints retained - Auto-save creates a version checkpoint every 5 minutes of activity - Edit positions must be adjusted for concurrent insertions/deletions (basic OT)
Output

Run or submit your code to see output here.