Implement undo() and redo() for set_cell on a Table class.
In an Airtable app you can create grid components, like spreadsheets, with columns, rows and cells; updates support undo/redo. You are given a Table class with filled-out functions. The table is represented internally as a 2D dict, e.g. {row1: {col1: value1, col2: value2}}, with a row_ids array and a column_ids array.
Functions: add_column, remove_column, add_row(row_id), remove_row(row_id), set_cell(row, col, value), get_cell(row, col), get_row_ids(), get_column_ids(). They already handle key errors by raising exceptions.
Iteration 1: implement undo() and redo() for the set_cell case. You may not add parameters to undo() or redo() (think about why, given the product context).
Test: table = Table() table.add_row("test_row") table.add_column("test_col") table.set_cell("test_row", "test_col", "initial_value") table.set_cell("test_row", "test_col", "changed_value") table.undo() # get_cell returns "initial_value" table.redo() # get_cell returns "changed_value"
Iteration 2 (not reached): extend undo()/redo() to add_row, remove_row, add_column and remove_column. The interviewer and candidate aligned on a diff-history approach to save space, and the candidate wrote test cases.
Expect follow-ups like
- Iteration 2: extend undo() and redo() to the other operations (add_row, remove_row, add_column, remove_column).
Community answers
Related questions
Implement a Singleton class.Related courses


