Design Excel Sum Formula - Problem

Design the basic function of Excel and implement the function of the sum formula.

Implement the Excel class:

  • Excel(int height, char width) Initializes the object with the height and the width of the sheet. The sheet is an integer matrix mat of size height x width with the row index in the range [1, height] and the column index in the range ['A', width]. All the values should be zero initially.
  • void set(int row, char column, int val) Changes the value at mat[row][column] to be val.
  • int get(int row, char column) Returns the value at mat[row][column].
  • int sum(int row, char column, List numbers) Sets the value at mat[row][column] to be the sum of cells represented by numbers and returns the value at mat[row][column]. This sum formula should exist until this cell is overlapped by another value or another sum formula.

Numbers format:

  • "ColRow" that represents a single cell. For example, "F7" represents the cell mat[7]['F'].
  • "ColRow1:ColRow2" that represents a range of cells. For example, "B3:F7" represents cells mat[i][j] for 3 ≤ i ≤ 7 and 'B' ≤ j ≤ 'F'.

Note: You could assume that there will not be any circular sum reference.

Input & Output

Example 1 — Basic Operations
$ Input: Excel(3,'C'), set(1,'A',2), sum(3,'C',["A1","A1:B2"]), set(2,'B',2), get(3,'C')
Output: [null,null,4,null,6]
💡 Note: Create 3x3 sheet, set A1=2, C3=SUM(A1+A1:B2)=2+2=4, set B2=2, get C3=2+4=6 (A1 counted once, range A1:B2 includes A1,B1,A2,B2)
Example 2 — Range Sum
$ Input: Excel(2,'B'), set(1,'A',1), set(1,'B',2), sum(2,'A',["A1:B1"]), get(2,'A')
Output: [null,null,null,3,3]
💡 Note: Create 2x2 sheet, A1=1, B1=2, A2=SUM(A1:B1)=1+2=3, get A2 returns 3
Example 3 — Formula Override
$ Input: Excel(1,'A'), sum(1,'A',[]), set(1,'A',5), get(1,'A')
Output: [null,0,null,5]
💡 Note: Create 1x1 sheet, A1=SUM([])=0, then set A1=5 (overrides formula), get A1 returns 5

Constraints

  • 1 ≤ height ≤ 26
  • 'A' ≤ width ≤ 'Z'
  • 1 ≤ row ≤ height
  • 'A' ≤ column ≤ width
  • -100 ≤ val ≤ 100
  • 1 ≤ numbers.length ≤ 5
  • numbers[i] has format "ColRow" or "ColRow1:ColRow2"
  • At most 100 calls will be made to set, get, and sum

Visualization

Tap to expand
INPUTALGORITHMRESULTA12B22C3SUM(A1,A1:B2)Excel Operations:set(1,'A',2)set(2,'B',2)sum(3,'C',["A1","A1:B2"])get(3,'C')1Parse cell references2Build dependency graph3Cache calculated values4Update dependents onlyDependency GraphA1, B1, A2, B2 -> C3Cached: C3 = 6Formula Result6Calculation:A1 = 2A1:B2 = A1+B1+A2+B2= 2+0+0+2 = 4Total = 2 + 4 = 6Efficiency:O(1) cached accessOnly update on changeKey Insight:Excel formulas create dependency relationships between cells. Smart caching andselective updates make operations efficient - only recalculate when dependencies change.TutorialsPoint - Design Excel Sum Formula | Optimized Dependency Tracking
Asked in
Google 15 Microsoft 25 Apple 10 Facebook 8
18.5K Views
Medium Frequency
~45 min Avg. Time
580 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen