Undo / Redo functionality in Text Editor + Multi-user scenario like Google Docs
Difficulty: MEDIUMCategory: LLDFrequency: 1Asked On 2025-03-01
Problem Statement
Design an Undo/Redo system for a multi-user collaborative editor where users can make concurrent edits on the same document, and each user can perform undo/redo operations while maintaining consistency across all collaborators. Focus is on OOP design, concurrency, and operation-based versioning.
Functional Requirements
1.Document Editing
•Multiple users can edit the same document simultaneously.
•Edits include insertion, deletion, formatting changes, etc.
2.Undo / Redo
•Each user can undo their last operations.
•Redo previously undone operations.
•Undo/Redo should not break document consistency for other users.
3.Operation Tracking
•Maintain a history of operations per user.
•Track metadata: timestamp, position, user ID, operation type.
4.Conflict Resolution
•Handle concurrent edits using Operational Transformation (OT) or CRDT.
•Ensure that undo/redo works correctly even when other users make concurrent changes.
5.Optional Features
•Support batch undo/redo (multiple operations at once).
•Versioning for document snapshots.
•Real-time notifications of undo/redo actions to collaborators.
Non-Functional Requirements
•Concurrency: Handle multiple users performing edits and undo/redo simultaneously.
•Low Latency: Reflect changes to all users in real-time.
•Consistency: Ensure document state remains consistent after undo/redo.
•Extensibility: Support additional operations like formatting, commenting, or image insertion.
Edge Cases & Constraints
•Undo after multiple concurrent edits → OT/CRDT needed to transform operations correctly.
•Redo after other users' edits → must reapply transformed operations.
•Network disconnect → handle offline operations and sync when reconnecting.
•Large document → maintain memory-efficient operation history.
Design Hints
•Key Classes / Interfaces:
•Document → stores content, operation history, and version.
•User → represents an editor with undo/redo stacks.
•Operation → encapsulates a change (insert, delete, format) with metadata.
•UndoRedoManager → manages per-user undo/redo stacks and applies transformations.
•CollaborationManager → handles concurrent edits and applies OT/CRDT.
•EventNotifier → propagates edits and undo/redo events to all collaborators.
•Data Structures:
•Stack<Operation> → per-user undo stack.
•Stack<Operation> → per-user redo stack.
•LinkedList / Rope → for efficient document text modifications.
•Queue of Operations → sequential processing of edits in OT/CRDT.
•Design Patterns:
•Command Pattern → represent operations for undo/redo.
•Observer Pattern → notify all users of document changes.
•Strategy Pattern → pluggable conflict resolution (OT or CRDT).
Example Flow
1.User A inserts "Hello" at position 0 → Operation created and pushed to undo stack.
2.User B inserts "World" at position 5 → concurrent operation transformed via OT.
3.User A performs undo → UndoRedoManager pops operation, transforms against concurrent ops, updates document.
4.EventNotifier pushes updated document state to all users.
5.User A performs redo → operation reapplied, document state synchronized.
Evaluation Areas
•Proper OOP modeling of operations, undo/redo stacks, and collaboration manager.
•Correct application of OT/CRDT to maintain consistency.
•Efficient real-time update propagation to all collaborators.
•Handling edge cases like concurrent edits, offline edits, and large documents.
This LLD problem tests OOP design, command pattern usage, concurrency handling, operational transformation, and multi-user consistency in collaborative applications.