KatInterview

Batch Spreadsheet Cell Evaluator with Cycle Rejection

Data Structure DesignAuto-graded · 15 test cases
MediumAuto-gradedData Structure Designgraphscycle-detectionexpression-evaluation

Last updated

Simulate setting and reading spreadsheet cells whose values are integers or sums of references, evaluating lazily and rejecting updates that would form cycles.

A spreadsheet holds named cells (uppercase letters followed by digits, e.g. A1, C12). Each cell is either empty or stores a definition: a literal integer, or a formula that adds together references to other cells and integer constants using + separators (for example "B1 + C1 + 5").

You are given a sequence of commands. Walk through them in order and produce one output per command.

Commands:

  • "setCell" with arguments [cell_name, value]. The value is either an int (a literal) or a str (a formula). Return True if the assignment is accepted, False if it is rejected. An assignment is rejected exactly when it would make the cell reachable from itself: following the references of formulas (transitively) starting at any reference in the new formula leads back to cell_name. This includes a formula that references the cell itself. A rejected assignment must leave the entire sheet exactly as it was before the command — no partial changes.
  • "getCell" with arguments [cell_name]. Return the current integer value of that cell. A cell that was never assigned, or whose value ultimately depends on an unassigned cell, contributes 0 for that unassigned cell. If a formula references the same cell more than once, that cell's value is added once per occurrence.

Parsing details for formulas:

  • The formula is split on +. Each piece is trimmed of surrounding whitespace.
  • A trimmed piece matching [A-Z]+[0-9]+ entirely is a reference.
  • Otherwise the trimmed piece must be a valid optionally-signed integer, e.g. "-3" or "12"; it contributes its integer value.
  • Constants may be negative or zero.

The output list contains, for each setCell, a True/False, and for each getCell, the evaluated integer.

Sample: ["setCell","setCell","setCell","getCell","setCell","getCell"] with [["B1",4],["C1",7],["A1","B1 + C1 + 5"],["A1"],["B1",10],["A1"]] yields [true,true,true,16,true,22].

Second sample: ["setCell","setCell","getCell","getCell","setCell","getCell"] with [["A1","B1 + C1"],["C1","A1"],["A1"],["C1"],["B1",3],["A1"]] yields [true,false,0,0,true,3] — the second assignment is rejected because it would form a cycle, so both A1 and C1 still evaluate to 0 until B1 is set.

Examples

Example 1

Input: run_spreadsheet_commands(['setCell', 'setCell', 'setCell', 'getCell', 'setCell', 'getCell'], [['B1', 4], ['C1', 7], ['A1', 'B1 + C1 + 5'], ['A1'], ['B1', 10], ['A1']])

Output: [True, True, True, 16, True, 22]

Example 2

Input: run_spreadsheet_commands(['setCell', 'setCell', 'getCell', 'getCell', 'setCell', 'getCell'], [['A1', 'B1 + C1'], ['C1', 'A1'], ['A1'], ['C1'], ['B1', 3], ['A1']])

Output: [True, False, 0, 0, True, 3]

Hints

Hint 1

Store each cell's definition as a list of terms (references or constants), then evaluate on demand with memoization per read.

Hint 2

An assignment forms a cycle exactly when the target cell is reachable from one of its new references by following forward formula edges. Check this before mutating anything so a rejection is atomic.

Hint 3

Duplicate references must count each occurrence; treat a literal integer as a formula with no references so you have only one storage shape.