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
Spreadsheet: Cell References and Formula EvaluationStep 1: Initialize Spreadsheet(3)A1B1C1A2B2C2Step 2: setCell("A1", 5), setCell("B2", 3)5B1C1A23C2Step 3: getValue("=A1+B2")=A1+B2Parse: A1 → 5, B2 → 3Result: 5 + 3 = 8Cell Reference Format: [A-Z][1-rows]Key Operations: setCell, resetCell, getValue🎯 **Key Insight:** Use hash map to store only set cells for memory efficiency
Understanding the Visualization
1
Initialize
Create empty spreadsheet with specified rows
2
Set Cells
Store values in specific cell references
3
Calculate
Evaluate formulas by parsing and summing operands
Key Takeaway
🎯 Key Insight: Store only modified cells in a hash map to save memory and achieve O(1) operations
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