Design Spreadsheet - Problem
Imagine building your own version of Microsoft Excel or Google Sheets! In this problem, you'll design a simplified spreadsheet system that can store values in cells and evaluate basic formulas.
Your spreadsheet has 26 columns labeled from 'A' to 'Z' and a specified number of rows. Each cell can hold an integer value between 0 and 105.
Key Features to Implement:
setCell(cell, value)- Set a cell's value (e.g., "A1", "B10")resetCell(cell)- Reset a cell back to 0getValue(formula)- Evaluate formulas like "=A1+B2" or "=5+A3"
Example Usage:
spreadsheet.setCell("A1", 10);
spreadsheet.setCell("B2", 20);
result = spreadsheet.getValue("=A1+B2"); // Returns 30The challenge lies in efficiently parsing cell references, storing the data, and evaluating formulas that can mix cell references with literal integers.
Input & Output
example_1.py โ Basic Operations
$
Input:
spreadsheet = Spreadsheet(5)
spreadsheet.setCell("A1", 10)
spreadsheet.setCell("B2", 20)
result = spreadsheet.getValue("=A1+B2")
โบ
Output:
30
๐ก Note:
We set A1 to 10 and B2 to 20, then evaluate the formula =A1+B2 which gives us 10+20=30
example_2.py โ Mixed Formula
$
Input:
spreadsheet = Spreadsheet(3)
spreadsheet.setCell("C1", 5)
result = spreadsheet.getValue("=C1+25")
โบ
Output:
30
๐ก Note:
We can mix cell references (C1=5) with literal integers (25) in formulas: 5+25=30
example_3.py โ Reset and Unset Cells
$
Input:
spreadsheet = Spreadsheet(2)
spreadsheet.setCell("A1", 15)
spreadsheet.resetCell("A1")
result = spreadsheet.getValue("=A1+B1+10")
โบ
Output:
10
๐ก Note:
After resetting A1 to 0 and with B1 never being set (defaults to 0), the formula =A1+B1+10 evaluates to 0+0+10=10
Constraints
- 1 โค rows โค 103
- Cell values are integers between 0 and 105
- Cell references follow format "[A-Z][1-rows]"
- Formulas are always in format "=X+Y" where X,Y are cell references or integers
- At most 104 operations will be performed
Visualization
Tap to expand
Understanding the Visualization
1
Smart Storage
Use a hash map as our 'catalog' - only record cells that have values
2
Instant Access
When we need a cell value, check our catalog. If it's not there, it's zero
3
Formula Magic
Parse formulas by splitting on '+' and looking up each part in our catalog
Key Takeaway
๐ฏ Key Insight: Use a hash map to create a 'smart catalog' that only stores what you need, achieving optimal space and time complexity for spreadsheet operations.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code