KatInterview

Cell Formula Evaluator with Cycle Detection

Trees & GraphsAuto-graded · 20 test cases
MediumAuto-gradedTrees & Graphsgraphsdfsrecursion

Last updated

Build a mini spreadsheet engine that evaluates formulas, detects circular dependencies, and handles dynamic updates.

Build a spreadsheet evaluator that processes a sequence of commands to store and retrieve cell values. Cells can hold integers or formulas that reference other cells.

You will implement a function that takes a list of operations and returns a list of results, one per operation.

Operations

Each operation is a list with 2 or 3 elements:

  • ["set", cell_id, content]: Store content in the cell identified by cell_id. Return "OK".
  • ["get", cell_id]: Compute and return the value of the cell. Return the value as a string in canonical decimal form (no leading zeros or plus signs; zero is "0"), or "CYCLE" if evaluation encounters a circular reference.

Cell Identifiers

Cell identifiers consist of one or more uppercase letters followed by a positive integer (no leading zeros). Examples: A1, Z99, AA10.

Cell Content

Content is either:

  • A signed integer string (e.g., "5", "-12", "0")
  • A formula beginning with =

Formula Syntax

A formula has the form term op term op term ... where:

  • Each term is either a non-negative integer literal or a cell identifier
  • Each op is + or -
  • Whitespace may appear between tokens

Formulas do not support parentheses or unary operators inside the expression.

Evaluation Rules

  • A cell that has never been set evaluates to 0
  • Each reference to a cell in a formula contributes independently. For example, =A1+A1 adds the value of A1 twice.
  • When a cell is set, its previous content is completely replaced
  • A get operation must detect if evaluating the target cell requires traversing a dependency cycle. If a cycle is encountered, return "CYCLE".
  • Cycles that are not reachable from the requested cell do not affect the result
  • Setting a cell to a new value may create or break cycles; the next get reflects the updated dependency graph

Constraints

  • At most 500 operations
  • At most 100 terms in any formula
  • All numeric literals, stored integers, and intermediate arithmetic results fit in a signed 32-bit integer
  • All inputs are syntactically valid

Examples

Example 1

Input: evaluate_sheet([['set', 'A1', '4'], ['set', 'B1', '=A1+A1-1'], ['get', 'B1'], ['set', 'A1', '8'], ['get', 'B1']])

Output: ['OK', 'OK', '7', 'OK', '15']

Example 2

Input: evaluate_sheet([['set', 'A1', '=B1+1'], ['set', 'B1', '=A1'], ['get', 'A1'], ['get', 'C1'], ['set', 'B1', '2'], ['get', 'A1']])

Output: ['OK', 'OK', 'CYCLE', '0', 'OK', '3']

Hints

Hint 1

Store each cell's raw content as-is and compute values on demand when a get is issued

Hint 2

Use depth-first traversal from the target cell through its dependencies; a cell visited while already being evaluated indicates a cycle

Hint 3

Track three states for each cell during evaluation: not yet seen, currently evaluating (on the recursion stack), and fully computed (memoized)