Design Spreadsheet - Problem

A spreadsheet is a grid with 26 columns (labeled from 'A' to 'Z') and a given number of rows. Each cell in the spreadsheet can hold an integer value between 0 and 10^5.

Implement the Spreadsheet class:

  • Spreadsheet(int rows) Initializes a spreadsheet with 26 columns (labeled 'A' to 'Z') and the specified number of rows. All cells are initially set to 0.
  • void setCell(String cell, int value) Sets the value of the specified cell. The cell reference is provided in the format "AX" (e.g., "A1", "B10"), where the letter represents the column (from 'A' to 'Z') and the number represents a 1-indexed row.
  • void resetCell(String cell) Resets the specified cell to 0.
  • int getValue(String formula) Evaluates a formula of the form "=X+Y", where X and Y are either cell references or non-negative integers, and returns the computed sum.

Note: If getValue references a cell that has not been explicitly set using setCell, its value is considered 0.

Input & Output

Example 1 — Basic Operations
$ Input: [["Spreadsheet",3],["setCell","A1",5],["setCell","B2",3],["getValue","=A1+B2"],["resetCell","A1"],["getValue","=A1+B2"]]
Output: [null,null,null,8,null,3]
💡 Note: Create 3-row spreadsheet, set A1=5 and B2=3, A1+B2=8, reset A1 to 0, now A1+B2=3
Example 2 — Formula with Constants
$ Input: [["Spreadsheet",2],["setCell","A1",10],["getValue","=A1+5"],["getValue","=2+3"]]
Output: [null,null,15,5]
💡 Note: Set A1=10, formula A1+5=10+5=15, formula 2+3=5
Example 3 — Unset Cell Reference
$ Input: [["Spreadsheet",1],["getValue","=A1+B1"]]
Output: [null,0]
💡 Note: Both A1 and B1 are unset, so they default to 0: 0+0=0

Constraints

  • 1 ≤ rows ≤ 1000
  • Cell references are valid format (A-Z)(1-rows)
  • 0 ≤ cell values ≤ 105
  • Formulas contain exactly 2 operands with '+' operator

Visualization

Tap to expand
Design Spreadsheet - Hash Map Approach INPUT A B C ... Z 1 5 0 2 0 3 3 26 cols x rows Operations: 1. Spreadsheet(3) 2. setCell("A1", 5) 3. setCell("B2", 3) 4. getValue("=A1+B2") 5. resetCell("A1") 6. getValue("=A1+B2") ALGORITHM STEPS 1 Initialize HashMap Store cells as "A1" --> value HashMap<String, Integer> "A1" --> 5 "B2" --> 3 O(1) access 2 setCell Operation map.put(cell, value) 3 resetCell Operation map.remove(cell) 4 getValue Formula Parse "=X+Y", evaluate sum "=A1+B2" --> parse X = "A1" --> getValue(A1) Y = "B2" --> getValue(B2) return X + Y Default: 0 if not set FINAL RESULT Execution Trace: 1. Spreadsheet(3) null 2. setCell("A1",5) null map: {"A1":5} 3. setCell("B2",3) null map: {"A1":5,"B2":3} 4. getValue("=A1+B2") 8 5 + 3 = 8 5. resetCell("A1") null map: {"B2":3} 6. getValue("=A1+B2") 3 0 + 3 = 3 (A1 not set = 0) Output Array: [null,null,null,8,null,3] OK - All operations verified Key Insight: Using a HashMap with cell references as keys (e.g., "A1") provides O(1) time complexity for all operations. For getValue: parse formula to extract operands, check if each is a cell ref or integer, then sum. Unset cells default to 0 (handled by map.getOrDefault). Space: O(number of set cells). TutorialsPoint - Design Spreadsheet | Hash Map Storage Approach
Asked in
Google 35 Microsoft 28 Amazon 22 Apple 18
25.0K Views
Medium Frequency
~25 min Avg. Time
890 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