Bird
0
0

You are designing a text editor with undo using the Command pattern. Which approach best supports multiple undo and redo operations efficiently?

hard📝 Trade-off Q15 of 15
LLD - Design — Tic-Tac-Toe Game
You are designing a text editor with undo using the Command pattern. Which approach best supports multiple undo and redo operations efficiently?
AUse two stacks: one for undo commands, one for redo commands
BStore all commands in a single list without pointers
COnly keep the last command for undo, discard others
DSave full document snapshots after each command
Step-by-Step Solution
Solution:
  1. Step 1: Understand undo/redo requirements

    Undo reverses last command, redo reapplies commands undone. Efficient support requires tracking both undo and redo history.
  2. Step 2: Evaluate data structures

    Two stacks allow pushing commands on execute, popping for undo, and pushing undone commands to redo stack. This supports multiple undo/redo efficiently.
  3. Final Answer:

    Use two stacks: one for undo commands, one for redo commands -> Option A
  4. Quick Check:

    Two stacks = efficient undo/redo [OK]
Quick Trick: Two stacks handle undo and redo efficiently [OK]
Common Mistakes:
MISTAKES
  • Using single list without tracking position
  • Keeping only last command loses history
  • Saving full snapshots wastes memory

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LLD Quizzes