Implement a spreadsheet with cell updates, undo and redo
Company: Airtable
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: easy
Interview Round: Other
Design a spreadsheet that supports setting cell values along with undo and redo operations. Each cell is addressed by a string key (for example `A1`, `B2`).
Any cell that has never been set is treated as having value `0`. Calling `set_cell` records the change in history so it can later be undone. Calling `undo` reverts the most recent `set_cell` (restoring the previous value), and `redo` re-applies the most recently undone change.
Any new `set_cell` call after an `undo` clears the redo history, since the timeline has diverged.
Implement the following methods:
- `set_cell(cell: str, value: int)`: Set the value of the given cell. Records the change so it can be undone, and clears any pending redo history.
- `get_cell(cell: str) -> int`: Return the current value of the cell. Returns 0 if the cell has never been set.
- `undo() -> bool`: Revert the most recent `set_cell` operation. Returns True if a change was undone, False if there was nothing to undo.
- `redo() -> bool`: Re-apply the most recently undone `set_cell` operation. Returns True if a change was redone, False if there was nothing to redo.
Example 1:
```text
Input:
["SpreadsheetUndoRedo", "set_cell", "set_cell", "get_cell", "undo", "get_cell", "redo", "get_cell", "undo", "undo", "undo"]
[[], ["A1", 5], ["A1", 10], ["A1"], [], ["A1"], [], ["A1"], [], [], []]
Output: [null, null, null, 10, true, 5, true, 10, true, true, false]
Explanation:
sheet = SpreadsheetUndoRedo() # empty spreadsheet
sheet.set_cell("A1", 5) # cells: {A1=5}
sheet.set_cell("A1", 10) # cells: {A1=10}
sheet.get_cell("A1") # → 10
sheet.undo() # → True (A1 reverts to 5)
sheet.get_cell("A1") # → 5
sheet.redo() # → True (A1 back to 10)
sheet.get_cell("A1") # → 10
sheet.undo() # → True (A1 reverts to 5)
sheet.undo() # → True (A1 reverts to 0, its initial value)
sheet.undo() # → False (nothing left to undo)
```
Example 2:
```text
Input:
["SpreadsheetUndoRedo", "set_cell", "set_cell", "undo", "set_cell", "redo", "get_cell", "get_cell"]
[[], ["B2", 100], ["C3", 200], [], ["B2", 50], [], ["B2"], ["C3"]]
Output: [null, null, null, true, null, false, 50, 0]
Explanation:
sheet = SpreadsheetUndoRedo() # empty spreadsheet
sheet.set_cell("B2", 100) # cells: {B2=100}
sheet.set_cell("C3", 200) # cells: {B2=100, C3=200}
sheet.undo() # → True (C3 reverts to 0)
sheet.set_cell("B2", 50) # diverges timeline, clears redo, cells: {B2=50, C3=0}
sheet.redo() # → False (redo stack was cleared by the new set_cell)
sheet.get_cell("B2") # → 50
sheet.get_cell("C3") # → 0 (was undone and never redone)
```
```hint What to record
For each set_cell, work out which values you would need to revert that change later, and which you would need to apply it again after an undo.
```
### Constraints & Assumptions
- `1 <= len(cell) <= 10`
- `-10⁹ <= value <= 10⁹`
- At most `10⁴` total operations across all methods.
- `undo` and `redo` return `True` if a change was applied, `False` otherwise.
### Clarifying Questions to Ask
- Should a `set_cell` that writes the value a cell already holds still create an undo entry?
- After undoing the first write to a cell, should the cell count as never set, or is it enough that it reads as 0?
- Is there a limit on how much history to keep, or must every change stay undoable for the whole session?
- Is the sheet used by one caller at a time, or do concurrent edits need handling?
### What a Strong Answer Covers
- What each history entry stores so a change can be both reverted and re-applied, including the first write to a cell.
- How undo and redo move a change between the undo history and the redo history.
- When the redo history is discarded, and why.
- The return values when there is nothing to undo or redo.
- The time and space cost of each method.
### Follow-up Questions
- How would you make a paste over a range of cells undo and redo as a single step?
- How would you bound memory if a session makes millions of edits?
- If cells can hold formulas that depend on other cells, what does undo need to restore and what does it need to recompute?
- With several people editing the same sheet, what should one person's undo do to a cell that someone else changed afterward?
Overview: Implement a spreadsheet that stores integer cell values and supports undo and redo of every change. The question tests what a change record must hold, how undo and redo interact, and how a new edit made after an undo discards the redo history.