Skip to main content
Back to problems
Leetcode
Medium
Design
Hash Maps
Arrays
Google
Microsoft
Design Spreadsheet

Design a spreadsheet data structure that supports setting and getting cell values, including simple formulas that add referenced cell values.

Acceptance 0%
Problem Statement

You need to implement a spreadsheet-like data structure with rows and columns. Each cell can store either a direct integer value or a formula value defined as the sum of other cells.

Support operations to:

  • set a cell to a direct value
  • get the current value of a cell
  • clear a cell
  • assign a formula to a cell as the sum of one or more referenced cells

A formula should automatically use the current values of the referenced cells. When a referenced cell changes later, the formula cell should reflect the updated result when queried again.

Input Format

This is a design problem. The interface typically supports operations such as:

  • setCell(row, col, value)
  • getCell(row, col)
  • clearCell(row, col)
  • sumCell(row, col, references)

Where references is a list of cell addresses or ranges used to compute a sum.

Output Format

Return the value of the requested cell for each read/query operation, using the spreadsheet rules described above.

Constraints

  • Cell addresses are finite and within the spreadsheet bounds given by the interface.
  • Values are integers.
  • Formula cells compute the sum of referenced cells at query time or via maintained state, depending on implementation.
  • If a cell is cleared, its value becomes 0.
  • Referencing the same cell multiple times should count multiple times.
Examples
Sample cases returned by the problem API.

Example 1

Input

Create spreadsheet
setCell(1, A, 5)
setCell(2, B, 3)
sumCell(3, C, [1A, 2B])
getCell(3, C)
setCell(1, A, 7)
getCell(3, C)

Output

8
10

Explanation

Cell C3 is the sum of A1 and B2. Initially 5 + 3 = 8. After A1 changes to 7, the formula result becomes 7 + 3 = 10.

Example 2

Input

Create spreadsheet
setCell(1, A, 4)
sumCell(2, A, [1A, 1A, 1A])
getCell(2, A)
clearCell(1, A)
getCell(2, A)

Output

12
0

Explanation

The same reference can appear multiple times in a formula. After clearing A1, its value becomes 0, so the formula also becomes 0.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.