Timers, games and history · senior
Undo and redo
Undo/redo keeps a past stack, the present and a future stack: a new action pushes the present onto past and clears future, undo moves present to future, redo moves it back; snapshots are simple, commands with inverses scale better and are needed for collaboration.
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceThe notes
What you must know
- Snapshot history: { past: State[], present, future: State[] }; immutable state with structural sharing keeps memory reasonable.
- Command pattern: store { do, undo } (or an operation and its inverse); less memory; needed when other users also change the document.
- Any new action clears the redo stack.
- Limit history length (drop the oldest); group rapid changes (typing within ~500 ms) into one undo step.
- Don't record actions that aren't 'content' (selection, hover, scroll).
- Shortcuts: Ctrl/Cmd+Z, Ctrl+Shift+Z or Ctrl+Y; disable buttons with canUndo/canRedo.
- Collaboration (Figma's rule): undo only your own changes; after undoing, copying and redoing, the document must be unchanged.
- A higher-order reducer undoable(reducer) adds history to any reducer.
The red pen
Common traps: mistake → fix
Mistake: Keep future after a new action
Fix: redo jumps to a branch that no longer makes sense.
Mistake: Store mutable references in history
Fix: later edits change old snapshots.
Mistake: Unlimited history of big snapshots
Fix: memory grows forever.
Mistake: A global undo in a shared document
Fix: undoing someone else's work.
Easy to hard
Questions interviewers ask, easy to hard
- Add undo and redo to this todo app / drawing app.
- Snapshots or commands? Trade-offs?
- How do you group typing into one undo step?
- How does undo work when several users edit the same document?
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceYour first interview is free
Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.