KatInterview

Eager Formula Sheet with Cycle-Safe Updates

Trees & GraphsAuto-graded · 16 test cases
MediumAuto-gradedTrees & Graphsdependency graphtopological sortcycle detection

Last updated

Simulate a mini spreadsheet where formulas are evaluated at write time, dependents update eagerly, and any write that would create a dependency cycle is rejected without changing state.

Eager Formula Sheet with Cycle-Safe Updates

You are given a sequence of spreadsheet operations and must simulate them on an in-memory sheet. The sheet supports two kinds of operations:

  • set_cell(label, value) writes a value into a cell. The value is either an integer or a formula string.
  • get_cell(label) reads a cell's current value.

Operations are given as two parallel lists: operations[i] is the operation name and arguments[i] is its argument list. Return one result per operation in a list, in order. Only set_cell and get_cell produce results: get_cell contributes its value, and set_cell contributes True if the write was accepted or False if it was rejected. Each operation's result is appended in order.

Labels

A label is one or more ASCII letters followed by one or more digits, such as "A1", "b12", or "XY3". Labels are case-insensitive, so "a1" and "A1" refer to the same cell. Anything that does not match this shape is invalid and must raise ValueError.

Values and formulas

  • An integer value (not a boolean) stores that constant in the cell.
  • A string beginning with = is a formula. Ignore all spaces in the formula. The remainder is split on + into terms. Each term is either a label (a reference to another cell) or an integer constant, which may have an optional leading - sign. Any other shape is invalid and must raise ValueError.
  • Any other value type (including booleans and non-= strings) must raise ValueError.

Semantics

  • A reference to a cell that has never been set contributes 0. A cell that has never been set reads as 0 through get_cell.
  • A formula's value is the sum of its terms, using the current values of the referenced cells.
  • Values are computed eagerly, at the moment of set_cell. get_cell never recomputes anything; it only reads stored values.
  • When a cell changes, every cell whose value depends on it, directly or transitively, must be brought up to date before set_cell returns. After every accepted write, every stored value must equal what a fresh evaluation of the sheet would produce.
  • If a write would create a dependency cycle, including a cell that references itself, set_cell returns False. In that case the sheet is left exactly as it was before the call, and no cell changes.
  • Unknown operation names must raise ValueError.

Constraints

  • 1 <= len(operations) == len(arguments) <= 2000
  • Each set_cell call has exactly two arguments: a label and a value. Each get_cell call has exactly one argument: a label.
  • Every formula has at most 50 terms.
  • Constants and stored integers fit in a 64-bit signed range, so Python's arbitrary-precision integers are sufficient.
  • Results are returned as a list containing booleans for set_cell and integers for get_cell.

Your function must return the list of results described above.

Examples

Example 1

Input: run_sheet_ops(['set_cell', 'set_cell', 'get_cell', 'set_cell', 'get_cell', 'set_cell', 'get_cell'], [['A1', 10], ['A2', '=A1+20'], ['A2'], ['A1', 1], ['A2'], ['A1', '=A2+1'], ['A1']])

Output: [True, True, 30, True, 21, False, 1]

Example 2

Input: run_sheet_ops(['set_cell', 'set_cell', 'get_cell', 'set_cell', 'get_cell'], [['A1', '=B1+1'], ['B1', '=A1+1'], ['A1'], ['B1', 7], ['A1']])

Output: [True, False, 1, True, 8]

Example 3

Input: run_sheet_ops(['set_cell', 'set_cell', 'set_cell', 'get_cell'], [['A1', '=B1+C1'], ['B1', 4], ['C1', 6], ['A1']])

Output: [True, True, True, 10]

Hints

Hint 1

Model the sheet as a directed graph where each formula cell points to the cells it references. A write is rejected exactly when the new formula can reach the target cell by following existing references.

Hint 2

Before committing a new formula, do a search from its referenced cells through the current formulas. If the search reaches the target cell, including a direct self-reference, reject the write without modifying anything.

Hint 3

After an accepted write, recompute values in dependency order. A topological ordering over the formula cells, such as Kahn's algorithm, guarantees each cell's references are already updated when it is evaluated.