Reactive Spreadsheet Grid
Problem
Design ReactiveSpreadsheetGrid for a fixed spreadsheet whose cells may hold signed integer literals or formulas that sum other cells. Calls are sequential on one shared instance.
Requirements
- Start every cell empty. Immediately after construction,
getValuereturnsEMPTYfor every cell. - Expose a stored literal. After
setLiteralcompletes,getValuereturns the supplied value as a signed base-10 integer string. - Remove formula edges on literal replacement.
setLiteralremoves every formula reference formerly owned by the target, so those edges cannot affect later evaluation or cycle detection. - Sum current dependency values. After a successful
setFormula,getValuereturns the signed base-10 sum of every referenced cell's current numeric value. - Propagate edits transitively. A later successful literal or formula edit is reflected by every direct or transitive formula dependent.
- Remove omitted formula dependencies. A successful
setFormulareplaces the target's complete prior reference set. An omitted former dependency no longer affects evaluation or cycle detection. - Reject circular formulas.
setFormulareturnsfalseexactly when installing the proposed complete reference set would create a direct or indirect cycle. Otherwise it installs the formula and returnstrue. - Keep cycle rejection atomic. When
setFormulareturnsfalse, every cell's content, references, and observable value remain exactly as they were before the call. - Propagate unavailable dependencies. A formula whose direct or transitive dependency is
EMPTYorDEPENDENCY_ERRORhas valueDEPENDENCY_ERROR. - Recover after dependencies become numeric. A formula previously returning
DEPENDENCY_ERRORreturns its numeric sum after later successful edits make every direct and transitive dependency numeric.
API
CellReference is an object with integer fields row and column that identifies an existing cell by zero-based coordinates.
| Signature | Returns | Behavior |
|---|---|---|
ReactiveSpreadsheetGrid(rows: integer, columns: integer) | Not applicable | Creates a fixed grid in which every cell is EMPTY. |
setLiteral(row: integer, column: integer, value: integer) | void | Replaces the target with a literal and removes all formula references formerly owned by it. |
setFormula(row: integer, column: integer, references: CellReference[]) | boolean | Replaces the target with an acyclic sum formula, or returns false without mutation if the replacement would create a cycle. |
getValue(row: integer, column: integer) | string | Returns EMPTY, DEPENDENCY_ERROR, or the current signed base-10 numeric value. |
Examples
For ReactiveSpreadsheetGrid(2, 3):
| Step | Operation | Result |
|---|---|---|
| 1 | getValue(0, 0) | "EMPTY" |
| 2 | setLiteral(0, 0, 4) | No return value |
| 3 | setLiteral(0, 1, 6) | No return value |
| 4 | setFormula(1, 0, [{row: 0, column: 0}, {row: 0, column: 1}]) | true |
| 5 | setFormula(1, 1, [{row: 1, column: 0}]) | true |
| 6 | getValue(1, 1) | "10" |
| 7 | setLiteral(0, 0, 20) | No return value |
| 8 | getValue(1, 1) | "26" |
A formula that depends on an empty cell returns DEPENDENCY_ERROR. If a proposed reverse reference would create a cycle, setFormula returns false and preserves the prior cells and graph.
Constraints
1 <= rows <= 50and1 <= columns <= 50- Every coordinate identifies an existing cell.
- Each
referencesarray contains 1 to 20 distinct existing cells. - A reference may identify the formula target; that call returns
false. - Every literal, intermediate sum, and formula result fits a signed 64-bit integer.
- At most 2,500 cells and 5,000 public method calls occur per testcase.
- Except for a cyclic proposed formula, invalid input is not supplied or judged.
Notes
EMPTY and DEPENDENCY_ERROR are reserved nonnumeric return strings. Formula-reference order does not affect a sum. No traversal or recomputation order is observable. Concurrent calls are outside scope.