Bird
Raised Fist0

What is the space complexity of storing commands in the brute force Command Pattern implementation with undo and redo stacks after executing n commands?

medium🪤 Complexity Trap Q6 of Q15
OOP & Design Patterns - Command Pattern - Undo/Redo, Request Queuing & Logging
What is the space complexity of storing commands in the brute force Command Pattern implementation with undo and redo stacks after executing n commands?
AO(1) auxiliary space since commands are executed in place
BO(n^2) due to storing command states multiple times
CO(n) space for storing executed commands in undo and redo stacks
DO(log n) space due to stack balancing
Step-by-Step Solution
Solution:
  1. Step 1: Count commands stored

    Each executed command is stored once in undo stack; redo stack stores undone commands.
  2. Step 2: Total space usage

    At most, all n commands are stored in stacks, so space is O(n).
  3. Final Answer:

    Option C -> Option C
  4. Quick Check:

    Stacks grow linearly with number of commands executed [OK]
Quick Trick: Undo and redo stacks store commands linearly [OK]
Common Mistakes:
MISTAKES
  • Forgetting stack space, assuming O(1)
  • Assuming quadratic space due to command states
Trap Explanation:
PITFALL
  • Candidates often forget that storing commands in stacks requires O(n) space proportional to executed commands.
Interviewer Note:
CONTEXT
  • Tests understanding of space complexity in command pattern implementations
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