Design a spreadsheet data structure that supports setting and getting cell values, including simple formulas that add referenced cell values.
You need to implement a spreadsheet-like data structure with rows and columns. Each cell can store either a direct integer value or a formula value defined as the sum of other cells.
Support operations to:
A formula should automatically use the current values of the referenced cells. When a referenced cell changes later, the formula cell should reflect the updated result when queried again.
This is a design problem. The interface typically supports operations such as:
setCell(row, col, value)getCell(row, col)clearCell(row, col)sumCell(row, col, references)Where references is a list of cell addresses or ranges used to compute a sum.
Return the value of the requested cell for each read/query operation, using the spreadsheet rules described above.
Example 1
Input
Create spreadsheet setCell(1, A, 5) setCell(2, B, 3) sumCell(3, C, [1A, 2B]) getCell(3, C) setCell(1, A, 7) getCell(3, C)
Output
8 10
Explanation
Cell C3 is the sum of A1 and B2. Initially 5 + 3 = 8. After A1 changes to 7, the formula result becomes 7 + 3 = 10.
Example 2
Input
Create spreadsheet setCell(1, A, 4) sumCell(2, A, [1A, 1A, 1A]) getCell(2, A) clearCell(1, A) getCell(2, A)
Output
12 0
Explanation
The same reference can appear multiple times in a formula. After clearing A1, its value becomes 0, so the formula also becomes 0.
Premium problem context
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.