Bird
Raised Fist0

In a Command Pattern implementation that maintains separate stacks for undo and redo operations, what is the average time complexity for performing an undo or redo action?

medium🧠 Conceptual Test Q5 of Q15
OOP & Design Patterns - Command Pattern - Undo/Redo, Request Queuing & Logging
In a Command Pattern implementation that maintains separate stacks for undo and redo operations, what is the average time complexity for performing an undo or redo action?
AO(n), because all commands must be re-executed to maintain state
BO(1), since undo and redo involve simple stack push/pop operations
CO(log n), due to searching through command history
DO(n^2), as undo and redo require nested loops over command stacks
Step-by-Step Solution
Solution:
  1. Step 1: Understand the data structures used

    Undo and redo stacks are typically implemented as simple stacks (LIFO).
  2. Step 2: Analyze undo/redo operations

    Undo pops the last command from the undo stack and pushes it onto the redo stack, and vice versa for redo.
  3. Final Answer:

    Option B -> Option B
  4. Quick Check:

    Stack push/pop operations are constant time [OK]
Quick Trick: Undo/redo stacks use push/pop, so operations are O(1) [OK]
Common Mistakes:
MISTAKES
  • Assuming undo requires re-executing all commands
  • Confusing time complexity with space complexity
  • Thinking redo requires searching through history
Trap Explanation:
PITFALL
  • Option B looks plausible if one thinks undo requires replaying commands, but stacks avoid this.
Interviewer Note:
CONTEXT
  • Tests understanding of time complexity in undo/redo stack operations.
Master "Command Pattern - Undo/Redo, Request Queuing & Logging" in OOP & Design Patterns

2 interactive learning modes - each teaches the same concept differently

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More OOP & Design Patterns Quizzes