KatInterview

Mini Spreadsheet: Cell Formulas and Cycle Detection

Data Structure DesignAuto-graded · 12 test cases

Last updated

Tests dependency graph modeling, cycle detection on writes, and correct value propagation across a simple formula-based spreadsheet engine.

Mini Spreadsheet: Cell Formulas and Cycle Detection

You are building the core logic for a tiny spreadsheet engine. The engine processes a sequence of operations and must return one result per operation.

Cell Names

A cell name is one or more letters followed by one or more digits, for example A1, B12, or AA3. Cell names are case-insensitive; a1 and A1 refer to the same cell.

Cell Content

Content stored in a cell is one of two forms:

  • A plain integer, such as 7 or 42.
  • A formula, which begins with = and adds together one or more terms separated by +. Each term is either a plain integer or a cell reference, for example =A1+B2+5.

Operations

Each operation is a string, and its arguments are a list of strings:

OperationArgumentsReturns
set_cell[cell, content]True if the write succeeded, False if it was rejected
get_cell[cell]The integer value of the cell

set_cell rules:

  • If storing the new content would create a circular dependency (directly or through a chain of references), the write is rejected: return False and leave the cell's previous content unchanged.
  • Otherwise, store the content and return True.

get_cell rules:

  • Return the computed integer value of the cell.
  • A cell that has never been written evaluates to 0.
  • If the cell holds a plain integer, return that integer.
  • If the cell holds a formula, evaluate it by summing each term, resolving any cell references recursively.

Circular Dependency Policy

A circular dependency occurs when following the chain of references from a cell eventually leads back to itself. Examples:

  • Setting A1 = =A1 is a self-cycle.
  • Setting A1 = =B1 while B1 = =A1 would form a two-cell cycle.

When a write would create a cycle, reject it entirely. The sheet must remain exactly as it was before that write was attempted.

Propagation

Because values are computed at read time by recursively resolving references, any update to a cell is automatically reflected the next time any cell that (directly or indirectly) depends on it is read. No extra propagation step is required.

Constraints

  • 1 ≤ len(operations) == len(arguments) ≤ 1000
  • Every operation string is either "set_cell" or "get_cell".
  • Cell names match [A-Za-z]+[0-9]+.
  • Formula terms are either valid cell names or integers matching [+-]?\d+.
  • Plain integer content and integer terms in formulas fit in a 32-bit signed integer.
  • At most 200 distinct cells are live at any one time.
  • There are no operators other than + in formulas.

Examples

Example 1

Input: spreadsheet_engine(['set_cell', 'set_cell', 'get_cell', 'set_cell', 'get_cell', 'get_cell'], [['A1', '7'], ['B1', '=A1+5'], ['B1'], ['A1', '10'], ['B1'], ['A1']])

Output: [True, True, 12, True, 15, 10]

Example 2

Input: spreadsheet_engine(['set_cell', 'set_cell', 'set_cell', 'set_cell', 'get_cell', 'get_cell'], [['A1', '=B1+1'], ['B1', '=A1'], ['B1', '=3'], ['A1', '=B1+1'], ['A1'], ['B1']])

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

Hints

Hint 1

Think of the cells as nodes in a directed graph where an edge from X to Y means 'X's formula references Y'. Cycle detection is then reachability: before accepting a write, check whether the cell being written is already reachable from any cell it would reference.

Hint 2

You only need to store the raw content string for each cell. When get_cell is called, walk the formula recursively to compute the value on the fly — this keeps set_cell simple and avoids a separate cache invalidation step.

Hint 3

When a set_cell is rejected, the sheet must be completely unchanged. Since you run the cycle check before touching the contents dictionary, a rejection naturally requires no rollback.