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 0
  • getValue(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 30

The 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
Library Catalog AnalogyPhysical Library (2D Array)Millions of empty shelvesReserved but unused spaceMemory: O(rows ร— 26)Smart Catalog (Hash Map)๐Ÿ“– "A1" โ†’ Book #10๐Ÿ“– "B2" โ†’ Book #20๐Ÿ“– "Z5" โ†’ Book #100โœ“ Only track what existsMemory: O(books_count)Formula Evaluation ProcessFormula: =A1+B2+15A1โ†’10B2โ†’2015โ†’15Result: 45Why Hash Map Wins:๐Ÿš€ O(1) access time - instant lookups๐Ÿ’พ Space efficient - no wasted memory๐Ÿ“ˆ Scales with data, not grid size๐ŸŽฏ Perfect for sparse spreadsheets
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.
Asked in
Google 45 Microsoft 38 Amazon 32 Meta 28
38.4K Views
Medium Frequency
~25 min Avg. Time
1.8K 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