Bird
Raised Fist0

Given the following undo and redo stacks after several command executions and undos, which command was most recently executed?

hard🔄 Reverse Engineer Q9 of Q15
OOP & Design Patterns - Command Pattern - Undo/Redo, Request Queuing & Logging
Given the following undo and redo stacks after several command executions and undos, which command was most recently executed? undo_stack (top to bottom): [AddCommand(2), AddCommand(5)] redo_stack (top to bottom): [AddCommand(3)]
AAddCommand with value 5
BAddCommand with value 0
CAddCommand with value 2
DAddCommand with value 3
Step-by-Step Solution
Solution:
  1. Step 1: Understand stack order

    undo_stack top is last executed command; redo_stack top is last undone command.
  2. Step 2: Identify last executed command

    undo_stack top is AddCommand(5), so it was most recently executed.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Undo stack top reflects last executed command [OK]
Quick Trick: Undo stack top is last executed command [OK]
Common Mistakes:
MISTAKES
  • Confusing redo stack top with last executed
  • Ignoring stack order semantics
Trap Explanation:
PITFALL
  • Candidates often mistake redo stack top as last executed command, but it holds undone commands.
Interviewer Note:
CONTEXT
  • Tests deep understanding of undo/redo stack states
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