Bird
0
0

You are designing a text editor with undo functionality using the Memento pattern. Which approach best balances memory usage and undo capability?

hard📝 Trade-off Q15 of 15
LLD - Behavioral Design Patterns — Part 2
You are designing a text editor with undo functionality using the Memento pattern. Which approach best balances memory usage and undo capability?
AStore a Memento only after significant changes or at checkpoints
BStore a Memento after every single character change
CStore all changes as raw text snapshots without Memento objects
DDo not store any state; rely on user to retype
Step-by-Step Solution
Solution:
  1. Step 1: Consider memory and undo tradeoff

    Storing a Memento after every character change (Store a Memento after every single character change) uses excessive memory and is inefficient.
  2. Step 2: Evaluate checkpoint strategy

    Storing Mementos after significant changes or checkpoints (Store a Memento only after significant changes or at checkpoints) reduces memory use while allowing meaningful undo steps.
  3. Step 3: Assess other options

    Store all changes as raw text snapshots without Memento objects wastes memory by storing raw snapshots without encapsulation; Do not store any state; rely on user to retype removes undo capability.
  4. Final Answer:

    Store a Memento only after significant changes or at checkpoints -> Option A
  5. Quick Check:

    Checkpoint Mementos balance memory and undo [OK]
Quick Trick: Save states at checkpoints, not every keystroke [OK]
Common Mistakes:
MISTAKES
  • Saving state too frequently causing memory bloat
  • Ignoring encapsulation by storing raw snapshots
  • Not implementing undo at all

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LLD Quizzes