Implement spreadsheet undo/redo operations
Spreadsheet Undo/Redo Operations
Design a data structure that simulates Undo/Redo functionality for a simplified spreadsheet editor.
A user performs a sequence of operations on cells, where each operation changes the value of a single cell.
Operations
SET cell value: Sets the specifiedcell(e.g., "A1") to a givenvalue(which can be a string or a number).UNDO k: Undoes the lastksuccessfulSEToperations. If fewer thankoperations are available to undo, all availableSEToperations are undone.REDO k: Redoes the lastkoperations that were previously undone. If fewer thankoperations are available to redo, all available undone operations are redone.
Rules
UNDOandREDOoperations exclusively affectSEToperations.- Executing a new
SEToperation after anUNDOoperation will clear the entire redo history. - If an
UNDO kcommand requests more steps than currently exist in the undo history, all available steps should be undone. - If a
REDO kcommand requests more steps than currently exist in the redo history, all available steps should be redone.
Input/Output
Given a list of commands, the system should process them and return the final mapping of cell -> value representing the state of the spreadsheet.
Example
Consider the following sequence of commands:
SET A1 10SET A1 20SET B2 5UNDO 2(This undoesB2=5, thenA1=20. The state before thisSET A1 20wasA1=10.)REDO 1(This redoesA1=20.)SET C1 7(This clears the redo history.)
The final state of the spreadsheet should be: A1=20, C1=7. Cell B2 would be empty.
Constraints
- The total number of commands (
N) can be up to 10^5. - The solution should aim for efficient time and memory complexity per command.
- Candidates are expected to discuss data structures and complexity, then implement the core logic.
AI interview prep
Core dilemma
Efficiently managing a mutable history of state changes with branching (redo history clearing) and arbitrary 'k' steps, while maintaining a consistent current spreadsheet state.
3 tradeoffs and 3 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Implement spreadsheet undo/redo operations
Spreadsheet Undo/Redo Operations
Design a data structure that simulates Undo/Redo functionality for a simplified spreadsheet editor.
A user performs a sequence of operations on cells, where each operation changes the value of a single cell.
Operations
SET cell value: Sets the specifiedcell(e.g., "A1") to a givenvalue(which can be a string or a number).UNDO k: Undoes the lastksuccessfulSEToperations. If fewer thankoperations are available to undo, all availableSEToperations are undone.REDO k: Redoes the lastkoperations that were previously undone. If fewer thankoperations are available to redo, all available undone operations are redone.
Rules
UNDOandREDOoperations exclusively affectSEToperations.- Executing a new
SEToperation after anUNDOoperation will clear the entire redo history. - If an
UNDO kcommand requests more steps than currently exist in the undo history, all available steps should be undone. - If a
REDO kcommand requests more steps than currently exist in the redo history, all available steps should be redone.
Input/Output
Given a list of commands, the system should process them and return the final mapping of cell -> value representing the state of the spreadsheet.
Example
Consider the following sequence of commands:
SET A1 10SET A1 20SET B2 5UNDO 2(This undoesB2=5, thenA1=20. The state before thisSET A1 20wasA1=10.)REDO 1(This redoesA1=20.)SET C1 7(This clears the redo history.)
The final state of the spreadsheet should be: A1=20, C1=7. Cell B2 would be empty.
Constraints
- The total number of commands (
N) can be up to 10^5. - The solution should aim for efficient time and memory complexity per command.
- Candidates are expected to discuss data structures and complexity, then implement the core logic.