Bird
Raised Fist0

Examine the following buggy Invoker code snippet implementing undo/redo stacks. Which line contains the subtle bug that can cause incorrect redo behavior after executing a new command?

medium🐞 Bug Identification Q14 of Q15
OOP & Design Patterns - Command Pattern - Undo/Redo, Request Queuing & Logging
Examine the following buggy Invoker code snippet implementing undo/redo stacks. Which line contains the subtle bug that can cause incorrect redo behavior after executing a new command?
ALine where redo_stack is not cleared after new command execution
BLine where redo_stack is appended in undo
CLine where command.execute() is called in execute_command
DLine where undo_stack is popped in undo
Step-by-Step Solution
Solution:
  1. Step 1: Identify expected redo stack behavior

    After executing a new command, redo stack must be cleared to avoid invalid redo commands.
  2. Step 2: Locate missing redo_stack.clear()

    The execute_command method lacks clearing redo_stack, so redo commands remain stale.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Redo stack not cleared after new command breaks redo correctness [OK]
Quick Trick: Redo stack must clear on new command execution [OK]
Common Mistakes:
MISTAKES
  • Forgetting to clear redo stack
  • Undoing commands in wrong order
Trap Explanation:
PITFALL
  • The missing redo_stack.clear() is subtle but critical; other lines look suspicious but are correct.
Interviewer Note:
CONTEXT
  • Checks candidate's attention to subtle state management bugs in undo/redo 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