Skip to the guide

Undo, redo and history

IntermediatePattern7 pages

When a prompt says "and add undo", you must choose what to remember for each edit: enough to reverse it, or a copy of everything. This move keeps two stacks of edit objects, and each edit stores only what its own reverse needs. It combines Command for edits that change a little with Memento for edits that change everything. Interviewers listen for how you weigh those two with numbers, and for what happens to redo after a new edit.

Moving furniture for a party

Think of two friends rearranging a living room for a party, and one of them keeps changing her mind. Her friend could take a photo of the whole room before each move, and that always works. But the room holds a hundred things, and only the sofa moved. So he writes one line in a notebook instead: "sofa, from the window to the door". To go back, he reads the top line and carries the sofa back.

Each line he has gone back over moves to a second pile, so that she can say "no, I liked it" and he can do it again. If she asks for something new instead, like moving the table, he throws that second pile away. Those lines describe a room that no longer exists.