This question evaluates a candidate's ability to design stateful data structures and manage operation history, focusing on correctness of undo/redo semantics, handling edge cases, and analyzing time and space complexity.
Design a data structure that simulates Undo/Redo for a simplified spreadsheet editor.
A user performs a sequence of operations on cells. Each operation changes the value of a single cell.
SET cell value
: set
cell
(e.g., "A1") to
value
(string or number).
UNDO k
: undo the last
k
successful
SET
operations (or as many as possible).
REDO k
: redo the last
k
operations that were undone (or as many as possible).
UNDO
and
REDO
affect only
SET
operations.
UNDO
, if a new
SET
is executed, the redo history is cleared.
UNDO k
requests more steps than exist, undo all available steps.
REDO k
requests more steps than exist, redo all available steps.
Given a list of commands, return the final mapping of cell -> value.
Commands:
SET A1 10
SET A1 20
SET B2 5
UNDO 2
(undo:
B2=5
, then
A1=20
)
REDO 1
(redo:
A1=20
)
SET C1 7
(clears redo stack)
Final state: A1=20, C1=7 (and B2 is empty).
N
commands (e.g., 10^5).
Discuss the data structures and complexity; implement the core logic.