Question presented to candidate: "Build a counter that starts at 0 with four buttons: divide by 2, minus 1, plus 1 and times 2. Every operation should be undoable and redoable, and below the counter show a history table of what was done, with the old and new values. If I undo a few steps and then press a new operation, the steps I undid should no longer be redoable. Add Reset, and keyboard shortcuts if you have time."
Requirements:
- A value starting at 0 and four operation buttons:
/2,-1,+1,x2. - Undo reverts the latest operation; Redo re-applies the latest undone one. Both are disabled when there is nothing to do.
- A new operation after an undo clears the redo history.
- A history table, newest first, showing the operation, the old value and the new value.
- Reset clears the value and all history.
- Bonus: Ctrl+Z / Cmd+Z to undo, Ctrl+Shift+Z or Ctrl+Y to redo; a cap on history length.
What a strong answer should cover:
- The past / present / future model, kept in one state object so the three can never drift apart.
- Undo moves the newest past entry to the front of the future; redo moves it back; a new action pushes to the past and empties the future.
- Storing operations with their before and after values (or snapshots of the value), and why that makes undo exact even for
/2. - Pure transition functions that are trivial to unit-test, and immutable updates.
- Keyboard shortcuts on the window with cleanup, and disabling Undo / Redo when their stacks are empty.
Clarifying questions expected:
- "Should Reset itself be undoable?"
- "Is there a limit on how much history to keep?"
- "Should
/2produce fractions, or round to an integer?"
Code / implementation expected: Yes. A working component with the undo/redo logic as pure functions, in about 30 minutes.