Implement simplified Excel with SET/GET/SUM
Fuse EnergySoftware Engineer · Technical ScreenLast updated
This question evaluates understanding of data structures and algorithms for modeling and implementing a simplified spreadsheet, covering dependency graphs, formula evaluation, incremental updates, and cycle detection.
Implement a simplified in-memory spreadsheet that supports three operations: SET, GET, and SUM, similar to basic Excel functionality.
Assumptions and requirements:
- The spreadsheet consists of cells identified by standard Excel-like coordinates:
- Columns are single uppercase letters from
'A'to'Z'. - Rows are integers from
1to1000. - A cell ID is a string such as
'A1','B2','Z1000'. - Each cell can either contain:
- A numeric value (integer), or
- A formula that is a sum of other cells or rectangular ranges of cells.
You must support the following operations:
- SET(cellId, value)
- Set the given cell to a numeric integer value.
- Any existing formula or previous value in that cell is overwritten.
- Return the value set.
- GET(cellId)
- Return the current evaluated integer value of the cell.
- If the cell contains a formula (from a
SUMoperation), its value is the sum of its referenced cells, recursively evaluated.
- SUM(cellId, refs)
cellIdis the target cell where we store a formula.refsis a list of references. Each reference is either:- A single cell ID, e.g.
'B2', or - A rectangular range in the form
'A1:B2', which represents all cells whose column is betweenAandBinclusive and whose row is between1and2inclusive. - Store in
cellIda formula that is defined as the sum of all cells inrefs(expanding ranges into the constituent cells). - Return the evaluated sum of that formula.
Behavioral example (your implementation should be consistent with this behavior):
SET('A1', 1)→ returns1SET('B2', 2)→ returns2SUM('C1', ['A1:B2', 'B2'])→ returns5SUM('C1', ['B2'])→ returns2SET('C1', 5)→ returns5GET('C1')→ returns5SUM('D1', ['C1', 'A1'])→ returns6SET('A1', 10)→ returns10GET('C1')→ returns5(SET('C1', 5)replaced C1's formula with the plain value 5, so changingA1does not affect it)GET('D1')→ returns15(D1is still the formulaC1 + A1, now5 + 10)
Additional requirements and edge cases:
- Updating a cell via
SETorSUMmust correctly affect any other cells whose formulas depend (directly or transitively) on that cell, so that subsequentGETcalls return up-to-date values. - There may be chains of dependencies, e.g.
A1used byB1, which is used byC1, etc. - You must handle potential cycles in formulas. For example, if
A1depends onB1and laterB1is defined to depend onA1, there is a cycle. - Define and implement a reasonable behavior for cyclic dependencies (e.g., detect cycles during evaluation or update and prevent them, or return an error/special behavior). Clearly state your chosen behavior in code comments.
- Aim for an efficient implementation that avoids recomputing large dependency graphs from scratch on every
GET.
Design and implement the data structures and methods to support these operations.
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.