What's a genuine practical cost of using the Memento pattern that you should be ready to discuss in an interview?
Memory usage is the honest answer. Every saved memento holds a full snapshot of the originator's state, and if that state is large or snapshots are taken frequently — say, on every keystroke in an editor — the history can grow large very quickly and stay alive for as long as the Caretaker holds it. Real systems usually manage this deliberately: capping history to a fixed number of entries and evicting the oldest one once the limit is hit, or only snapshotting at meaningful checkpoints rather than on every single change. There's a second, related cost worth mentioning: if any of the originator's fields are mutable objects rather than immutable values, saving them correctly requires a deep copy rather than just storing a reference, which adds both memory and CPU overhead on every save.
Ready to master this question?
Generate a complete walkthrough — background, the full answer in plain language, a working code example explained line by line, a real-world scenario, common mistakes, and how this same question gets asked in different ways.
Sign in to generate a response