Design a Spreadsheet With Undo and Redo Across Cell Edits and Row/Column Changes

Quick Overview

Implement a 2D spreadsheet class whose cell edits and row and column inserts and deletes can all be undone and redone. It tests recording inverse actions on two stacks, clearing redo history after a new edit, keeping stored indices and the grid shape consistent, and reasoning about the cost of each operation.

Design a Spreadsheet With Undo and Redo Across Cell Edits and Row/Column Changes

Company: Airtable

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: medium

Interview Round: Other

Design a data structure `SpreadsheetWithUndoRedo` that simulates a 2D spreadsheet initialized with a given number of rows and columns, where every cell starts at `0`. The spreadsheet must support cell edits as well as structural changes such as inserting and deleting rows and columns. Every state-changing operation must be undoable and redoable. Performing a new state-changing operation after one or more `undo()` calls must clear the redo history, matching the behavior of standard editors. Implement the following methods: - `set_cell(row: int, col: int, value: int)`: Set the cell at (row, col) to value. Pushes an undo entry and clears the redo stack. - `get_cell(row: int, col: int) -> int`: Return the current value at (row, col). Does not affect undo or redo history. - `add_row(index: int)`: Insert a new row of zeros at the given index. Pushes an undo entry and clears the redo stack. - `delete_row(index: int)`: Delete the row at the given index. Pushes an undo entry (storing the removed row) and clears the redo stack. - `add_column(index: int)`: Insert a new column of zeros at the given index. Pushes an undo entry and clears the redo stack. - `delete_column(index: int)`: Delete the column at the given index. Pushes an undo entry (storing the removed column) and clears the redo stack. - `undo() -> bool`: Revert the most recent state-changing operation. Returns True if an action was undone, False if the undo stack was empty. - `redo() -> bool`: Reapply the most recently undone operation. Returns True if an action was redone, False if the redo stack was empty. Example 1, cell edits and a redo history cleared by a new edit: ```text s = SpreadsheetWithUndoRedo(2, 2) s.set_cell(0, 0, 5) s.set_cell(1, 1, 7) s.get_cell(0, 0) # 5 s.undo() # True, cell (1, 1) is 0 again s.get_cell(1, 1) # 0 s.redo() # True, cell (1, 1) is 7 again s.get_cell(1, 1) # 7 s.undo() # True s.set_cell(0, 1, 3) # a new edit clears the redo history s.redo() # False ``` Example 2, cell edits mixed with structural changes (the comments show the grid after each call): ```text s = SpreadsheetWithUndoRedo(2, 3) s.set_cell(1, 2, 9) # [[0, 0, 0], [0, 0, 9]] s.delete_row(0) # [[0, 0, 9]] s.get_cell(0, 2) # 9 s.add_column(0) # [[0, 0, 0, 9]] s.get_cell(0, 3) # 9 s.undo() # True, column 0 removed: [[0, 0, 9]] s.undo() # True, row 0 restored: [[0, 0, 0], [0, 0, 9]] s.get_cell(1, 2) # 9 s.redo() # True, row 0 deleted again: [[0, 0, 9]] s.redo() # True, column 0 inserted again: [[0, 0, 0, 9]] s.redo() # False ``` ```hint What to record For each operation, ask what you would need to put the sheet back exactly as it was. An insert can be reverted from its index alone, while a delete throws data away. ``` ```hint Shape bookkeeping Undo always reverts the newest action first, so think about what shape the sheet has at that moment. Then check what your code believes the column count is after every row has been deleted. ``` ### Constraints & Assumptions - `1 <= rows, cols <= 100` at initialization. - `-10⁹ <= value <= 10⁹` for any cell value. - `0 <= row < currentRows` and `0 <= col < currentCols` for `set_cell` and `get_cell`. - `0 <= index <= currentRows` for `add_row` and `0 <= index < currentRows` for `delete_row` (analogous for columns). - At most `10⁴` total operations across all method calls. ### Clarifying Questions to Ask - Can deletes leave the sheet with zero rows or zero columns, and must a later insert still know the other dimension? - Should an invalid index raise an error, and must a failed call leave the history unchanged? - Is the undo history unbounded, or should it keep only the most recent N actions? - Should several edits made together (a paste of a range) undo as one step, or is every method call its own step? ### What a Strong Answer Covers - What each operation must record so it can be reverted and replayed, including the full row or column a delete removes. - Correct movement of actions between the undo and redo stacks, with redo cleared on every new state-changing call and `get_cell` leaving history alone. - Why stored indices stay valid when undo runs newest first, and how the sheet keeps its width when every row is deleted. - The time and space cost of each operation, including a column insert on a row-major grid. - Working code checked against a sequence that mixes cell edits with structural changes. ### Follow-up Questions - How would you cap the memory used by history when a single deleted column can hold thousands of values? - How would you make a multi-cell paste undo and redo as one step? - If cells held formulas that reference other cells, what extra state would deleting and restoring a row need? - How would you make column insertion cheaper than touching every row?

Overview: Implement a 2D spreadsheet class whose cell edits and row and column inserts and deletes can all be undone and redone. It tests recording inverse actions on two stacks, clearing redo history after a new edit, keeping stored indices and the grid shape consistent, and reasoning about the cost of each operation.

Airtable logo
Airtable
Nov 24, 2026
mediumSoftware EngineerOtherSoftware Engineering Fundamentals
0
0

Design a data structure SpreadsheetWithUndoRedo that simulates a 2D spreadsheet initialized with a given number of rows and columns, where every cell starts at 0.

The spreadsheet must support cell edits as well as structural changes such as inserting and deleting rows and columns. Every state-changing operation must be undoable and redoable.

Performing a new state-changing operation after one or more undo() calls must clear the redo history, matching the behavior of standard editors.

Implement the following methods:

  • set_cell(row: int, col: int, value: int) : Set the cell at (row, col) to value. Pushes an undo entry and clears the redo stack.
  • get_cell(row: int, col: int) -> int : Return the current value at (row, col). Does not affect undo or redo history.
  • add_row(index: int) : Insert a new row of zeros at the given index. Pushes an undo entry and clears the redo stack.
  • delete_row(index: int) : Delete the row at the given index. Pushes an undo entry (storing the removed row) and clears the redo stack.
  • add_column(index: int) : Insert a new column of zeros at the given index. Pushes an undo entry and clears the redo stack.
  • delete_column(index: int) : Delete the column at the given index. Pushes an undo entry (storing the removed column) and clears the redo stack.
  • undo() -> bool : Revert the most recent state-changing operation. Returns True if an action was undone, False if the undo stack was empty.
  • redo() -> bool : Reapply the most recently undone operation. Returns True if an action was redone, False if the redo stack was empty.

Example 1, cell edits and a redo history cleared by a new edit:

s = SpreadsheetWithUndoRedo(2, 2)
s.set_cell(0, 0, 5)
s.set_cell(1, 1, 7)
s.get_cell(0, 0)     # 5
s.undo()             # True, cell (1, 1) is 0 again
s.get_cell(1, 1)     # 0
s.redo()             # True, cell (1, 1) is 7 again
s.get_cell(1, 1)     # 7
s.undo()             # True
s.set_cell(0, 1, 3)  # a new edit clears the redo history
s.redo()             # False

Example 2, cell edits mixed with structural changes (the comments show the grid after each call):

s = SpreadsheetWithUndoRedo(2, 3)
s.set_cell(1, 2, 9)  # [[0, 0, 0], [0, 0, 9]]
s.delete_row(0)      # [[0, 0, 9]]
s.get_cell(0, 2)     # 9
s.add_column(0)      # [[0, 0, 0, 9]]
s.get_cell(0, 3)     # 9
s.undo()             # True, column 0 removed: [[0, 0, 9]]
s.undo()             # True, row 0 restored: [[0, 0, 0], [0, 0, 9]]
s.get_cell(1, 2)     # 9
s.redo()             # True, row 0 deleted again: [[0, 0, 9]]
s.redo()             # True, column 0 inserted again: [[0, 0, 0, 9]]
s.redo()             # False

Constraints & Assumptions

  • 1 <= rows, cols <= 100 at initialization.
  • -10⁹ <= value <= 10⁹ for any cell value.
  • 0 <= row < currentRows and 0 <= col < currentCols for set_cell and get_cell .
  • 0 <= index <= currentRows for add_row and 0 <= index < currentRows for delete_row (analogous for columns).
  • At most 10⁴ total operations across all method calls.

Clarifying Questions to Ask Guidance

  • Can deletes leave the sheet with zero rows or zero columns, and must a later insert still know the other dimension?
  • Should an invalid index raise an error, and must a failed call leave the history unchanged?
  • Is the undo history unbounded, or should it keep only the most recent N actions?
  • Should several edits made together (a paste of a range) undo as one step, or is every method call its own step?

What a Strong Answer Covers Guidance

  • What each operation must record so it can be reverted and replayed, including the full row or column a delete removes.
  • Correct movement of actions between the undo and redo stacks, with redo cleared on every new state-changing call and get_cell leaving history alone.
  • Why stored indices stay valid when undo runs newest first, and how the sheet keeps its width when every row is deleted.
  • The time and space cost of each operation, including a column insert on a row-major grid.
  • Working code checked against a sequence that mixes cell edits with structural changes.

Follow-up Questions Guidance

  • How would you cap the memory used by history when a single deleted column can hold thousands of values?
  • How would you make a multi-cell paste undo and redo as one step?
  • If cells held formulas that reference other cells, what extra state would deleting and restoring a row need?
  • How would you make column insertion cheaper than touching every row?