Problem Statement
Design a simplified version of Microsoft Word that supports:
- Multiple font types (Arial, Times New Roman, etc.)
- Font styles (Bold, Italic, Underline) — can be combined
- Font sizes
- Text formatting and paragraphs
- Inline images
- Undo / Redo functionality
The design should be extensible — adding new formatting options or features should not require modifying existing code.
Requirements
- Support rich text with mixed formatting within a single paragraph
- Images can be placed inline with text
- Undo/Redo should work for any operation (typing, formatting changes, image insertion)
- Design for extensibility (Open/Closed principle)
What the Interviewer Expects
-
Document structure — Composite Pattern:
DocumentcontainsPage[]PagecontainsBlock[](paragraphs, images)ParagraphcontainsTextRun[](each run has uniform formatting)TextRunhas content +FormattingStyle
-
Formatting — Strategy/Decorator Pattern:
FormattingStyleobject with font, size, bold, italic, underline- Styles can be composed/stacked
- New styles added without changing existing code
-
Undo/Redo — Command Pattern:
- Every action is a
Commandobject:execute()andundo() CommandHistorymaintains two stacks (undo stack, redo stack)- Executing new action clears redo stack
- Types:
InsertTextCommand,DeleteTextCommand,FormatCommand,InsertImageCommand
- Every action is a
-
Extensibility:
- Adding "Strikethrough" = new style flag, no existing code changes
- Adding "Table" = new
Blocksubclass, composites handle it naturally
Follow-ups
- How would you handle collaborative editing (multiple cursors)?
- How would you persist the document? What serialization format?
- How would you implement "group undo" (undo a bulk formatting change as one step)?
- What's the memory cost of storing every command? How would you optimize for large documents?
- How would you add a plugin system for third-party formatting extensions?