Implement simplified Excel with SET/GET/SUM
Simplified In-Memory Spreadsheet
Implement a simplified in-memory spreadsheet that supports three core operations: SET, GET, and SUM, mirroring basic Excel functionality.
Assumptions and Requirements
- Cell Identification: Cells are identified by standard Excel-like coordinates.
- Columns are single uppercase letters from 'A' to 'Z'.
- Rows are integers from 1 to 1000.
- A cell ID is a string, e.g., 'A1', 'B2', 'Z1000'.
- Cell Content: Each cell can contain either:
- A numeric integer value.
- A formula representing the sum of other cells or rectangular ranges of cells.
Operations
-
SET(cellId, value)
- Assigns a numeric integer
valueto the specifiedcellId. - Any existing formula or previous value in
cellIdis overwritten. - Returns the
valuethat was set.
- Assigns a numeric integer
-
GET(cellId)
- Returns 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)
- Stores a formula in
cellId. This formula is defined as the sum of all cells specified inrefs. refsis a list of references, where each reference can be:- A single cell ID (e.g., 'B2').
- A rectangular range in the format 'A1:B2', representing all cells whose column is between 'A' and 'B' (inclusive) and whose row is between 1 and 2 (inclusive).
- Returns the evaluated sum of the newly stored formula.
- Stores a formula in
Behavioral Example
The implementation should be consistent with the following sequence, demonstrating dependency updates:
SET('A1', 1)→ returns1SET('B2', 2)→ returns2SUM('C1', ['A1', 'B2'])→ returns3(C1's formula is A1 + B2)SUM('D1', ['C1', 'A1'])→ returns4(D1's formula is C1 + A1)SET('A1', 10)→ returns10GET('D1')→ returns22(D1 evaluates to (A1+B2) + A1 = (10+2) + 10 = 22)
Additional Requirements and Edge Cases
- Dependency Updates: Updating a cell (via
SETorSUM) must correctly affect any other cells whose formulas depend (directly or transitively) on that cell. SubsequentGETcalls must return up-to-date values. - Dependency Chains: The system must handle chains of dependencies (e.g., A1 used by B1, which is used by C1, etc.).
- Cycle Detection: You must handle potential cycles in formulas (e.g., A1 depends on B1, and B1 later depends on A1).
- 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.
- Efficiency: 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.
AI interview prep
Core dilemma
Efficiently managing complex, dynamic cell dependencies (including ranges and transitive links) in an in-memory system, ensuring immediate consistency while avoiding redundant computations and handling cyclic references gracefully.
3 tradeoffs and 3 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Implement simplified Excel with SET/GET/SUM
Simplified In-Memory Spreadsheet
Implement a simplified in-memory spreadsheet that supports three core operations: SET, GET, and SUM, mirroring basic Excel functionality.
Assumptions and Requirements
- Cell Identification: Cells are identified by standard Excel-like coordinates.
- Columns are single uppercase letters from 'A' to 'Z'.
- Rows are integers from 1 to 1000.
- A cell ID is a string, e.g., 'A1', 'B2', 'Z1000'.
- Cell Content: Each cell can contain either:
- A numeric integer value.
- A formula representing the sum of other cells or rectangular ranges of cells.
Operations
-
SET(cellId, value)
- Assigns a numeric integer
valueto the specifiedcellId. - Any existing formula or previous value in
cellIdis overwritten. - Returns the
valuethat was set.
- Assigns a numeric integer
-
GET(cellId)
- Returns 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)
- Stores a formula in
cellId. This formula is defined as the sum of all cells specified inrefs. refsis a list of references, where each reference can be:- A single cell ID (e.g., 'B2').
- A rectangular range in the format 'A1:B2', representing all cells whose column is between 'A' and 'B' (inclusive) and whose row is between 1 and 2 (inclusive).
- Returns the evaluated sum of the newly stored formula.
- Stores a formula in
Behavioral Example
The implementation should be consistent with the following sequence, demonstrating dependency updates:
SET('A1', 1)→ returns1SET('B2', 2)→ returns2SUM('C1', ['A1', 'B2'])→ returns3(C1's formula is A1 + B2)SUM('D1', ['C1', 'A1'])→ returns4(D1's formula is C1 + A1)SET('A1', 10)→ returns10GET('D1')→ returns22(D1 evaluates to (A1+B2) + A1 = (10+2) + 10 = 22)
Additional Requirements and Edge Cases
- Dependency Updates: Updating a cell (via
SETorSUM) must correctly affect any other cells whose formulas depend (directly or transitively) on that cell. SubsequentGETcalls must return up-to-date values. - Dependency Chains: The system must handle chains of dependencies (e.g., A1 used by B1, which is used by C1, etc.).
- Cycle Detection: You must handle potential cycles in formulas (e.g., A1 depends on B1, and B1 later depends on A1).
- 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.
- Efficiency: 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.