KatInterview

Linked Cell Grid with Cycle-Safe Formula Updates

Data Structure DesignAuto-graded · 13 test cases
MediumAuto-gradedData Structure Designgraphcycle-detectionsimulation

Last updated

Simulate a 26x26 grid whose cells hold integers or one-reference offset formulas, answering lookups lazily and rejecting any formula assignment that would close a dependency loop.

Problem

You are building a tiny spreadsheet. Its columns are A through Z and its rows are 1 through 26. Each cell can be in one of three states:

  • empty (never assigned),
  • holding a literal integer, or
  • holding a formula of the form target = source + offset, where source is another cell (or the cell itself) and offset is an integer.

A formula always reads the current content of its source cell, so any later change to a cell is immediately visible to every formula that depends on it, directly or through a chain.

Write a function that processes a list of commands in order and returns one output string per command.

Commands

Each command is a list whose first element is the operation name.

  • ["SET_VALUE", cell, value] Store the integer value in cell, replacing whatever it held before (a literal or a formula). Output "OK".

  • ["SET_FORMULA", cell, source_cell, offset] Make cell hold the formula source_cell + offset, replacing its previous content.

    • If this assignment would produce a circular dependency (including cell referring to itself, directly or through any chain of formulas), the command is rejected: the spreadsheet stays exactly as it was, including the previous content of cell, and the output is "CIRCULAR".
    • Otherwise the formula is stored and the output is "OK".
  • ["GET", cell] Output the current integer value of cell as a decimal string (for example "30" or "-7"). If cell is empty, or following its chain of formulas reaches an empty cell, output "UNSET".

Notes

  • Cell names are case-insensitive: "b1" and "B1" denote the same cell. Every address in the input is well formed.
  • A cell referenced by a formula may be empty at the time the formula is stored. That is allowed; the formula simply evaluates to "UNSET" until the source gets a value.
  • Return the list of output strings in the same order as the commands.

Constraints

  • 1 <= len(commands) <= 100000
  • Literal values and offsets are integers in [-10^9, 10^9].
  • Every evaluated result fits in a signed 64-bit integer.
  • All cell addresses are valid and lie within A1 to Z26 (case-insensitive).

Examples

Example 1

Input: simulate_cell_grid([['SET_VALUE', 'A1', 20], ['SET_FORMULA', 'B1', 'A1', 10], ['GET', 'B1'], ['SET_FORMULA', 'A1', 'B1', 0], ['GET', 'A1']])

Output: ['OK', 'OK', '30', 'CIRCULAR', '20']

Hints

Hint 1

Each formula names exactly one source cell, so every cell has at most one outgoing dependency. The dependencies therefore form simple chains rather than a general graph.

Hint 2

To decide whether `cell = source + k` creates a loop, walk the chain that starts at `source`. If you ever arrive at `cell`, the update is circular. Starting the walk at `source == cell` handles self-reference automatically.

Hint 3

You do not need to push updates to dependents. Store literals and formulas as they are and compute a value on demand by following the chain, adding offsets until you hit a literal (or an empty cell, which gives UNSET). Only commit a new formula after the cycle check passes.