Bird
Raised Fist0

What is an effective way to modify the Command pattern to support both undo and redo operations?

hard📝 Trade-off Q8 of Q15
LLD - Design — Tic-Tac-Toe Game
What is an effective way to modify the Command pattern to support both undo and redo operations?
AUse a hashmap to map commands to their inverse operations
BStore all commands in a single queue and replay them for redo
CImplement redo by calling execute() on the last undone command without tracking history
DMaintain two stacks: one for undo and one for redo commands
Step-by-Step Solution
Solution:
  1. Step 1: Understand undo/redo requirements

    Undo reverses last command; redo reapplies commands undone.
  2. Step 2: Analyze data structures

    Two stacks allow pushing commands on execute and undo, and moving commands between stacks for redo.
  3. Final Answer:

    Maintain two stacks: one for undo and one for redo commands -> Option D
  4. Quick Check:

    Two stacks efficiently support undo/redo [OK]
Quick Trick: Two stacks enable undo and redo [OK]
Common Mistakes:
MISTAKES
  • Using a single queue for redo
  • Calling execute() without history for redo
  • Mapping commands to inverses without stacks

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LLD Quizzes