SystemDrills

Session: Sign in to solve

Solution.txt

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, getValue returns EMPTY for every cell.
  • Expose a stored literal. After setLiteral completes, getValue returns the supplied value as a signed base-10 integer string.
  • Remove formula edges on literal replacement. setLiteral removes 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, getValue returns 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 setFormula replaces the target's complete prior reference set. An omitted former dependency no longer affects evaluation or cycle detection.
  • Reject circular formulas. setFormula returns false exactly when installing the proposed complete reference set would create a direct or indirect cycle. Otherwise it installs the formula and returns true.
  • Keep cycle rejection atomic. When setFormula returns false, 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 EMPTY or DEPENDENCY_ERROR has value DEPENDENCY_ERROR.
  • Recover after dependencies become numeric. A formula previously returning DEPENDENCY_ERROR returns 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.

SignatureReturnsBehavior
ReactiveSpreadsheetGrid(rows: integer, columns: integer)Not applicableCreates a fixed grid in which every cell is EMPTY.
setLiteral(row: integer, column: integer, value: integer)voidReplaces the target with a literal and removes all formula references formerly owned by it.
setFormula(row: integer, column: integer, references: CellReference[])booleanReplaces the target with an acyclic sum formula, or returns false without mutation if the replacement would create a cycle.
getValue(row: integer, column: integer)stringReturns EMPTY, DEPENDENCY_ERROR, or the current signed base-10 numeric value.

Examples

For ReactiveSpreadsheetGrid(2, 3):

StepOperationResult
1getValue(0, 0)"EMPTY"
2setLiteral(0, 0, 4)No return value
3setLiteral(0, 1, 6)No return value
4setFormula(1, 0, [{row: 0, column: 0}, {row: 0, column: 1}])true
5setFormula(1, 1, [{row: 1, column: 0}])true
6getValue(1, 1)"10"
7setLiteral(0, 0, 20)No return value
8getValue(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 <= 50 and 1 <= columns <= 50
  • Every coordinate identifies an existing cell.
  • Each references array 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.

PRIVATE WORKSPACE

Checking your session…

The statement is public. The editor, editorial, submissions, and saved work are private.