KatInterview

In-Memory Cell Grid with Formula Chaining

Data Structure DesignAuto-graded · 23 test cases
HardAuto-gradedData Structure Designdesigngraphssimulation

Last updated

Tests dependency graph design, lazy cache invalidation, recursive formula evaluation, and cycle detection in a grid of named cells.

In-Memory Cell Grid with Formula Chaining

You are building a minimal in-memory grid engine. Each cell in the grid has a string address: one uppercase letter (A–Z) for the column and an integer (1–1000) for the row, e.g. A1, B2, Z1000.

A cell holds either a plain integer or a derived formula (the sum of other cells). Uninitialized cells have the value 0.


Operations

Your engine must process a sequence of operations given as two parallel lists:

  • operations: a list of strings, each one of "SET", "GET", or "SUM".
  • arguments: a list of argument lists, one per operation.

Return a list of results, one per operation, in order.


SET

Arguments: [address, number]

Assigns the integer number to the cell at address. Any formula previously stored there is discarded, and all dependency edges from that cell are removed.

Returns: the integer that was just stored.


GET

Arguments: [address]

Returns the current integer value of the cell at address.

  • If the cell holds a plain integer, return it directly.
  • If the cell holds a formula, evaluate it recursively: a formula cell's value is the sum of the current values of every cell it references (with duplicates counted separately, uninitialized cells contributing 0).
  • If evaluation encounters a cycle (directly or transitively), return null / None for every cell involved in or depending on that cycle.

Returns: an integer, or null/None if the cell is part of a cycle.


SUM

Arguments: [address, refs]

Stores a summation formula in the cell at address. refs is a list of reference strings. Each reference is either:

  • A single address, e.g. "B2", or
  • A rectangular range "X1:Y2", representing every cell whose column is between X and Y (inclusive) and whose row is between the two row numbers (inclusive). Column and row bounds may appear in either order, e.g. "B2:A1" is valid and covers the same cells as "A1:B2".

Duplicates within refs (or produced by overlapping ranges) are not deduplicated — each occurrence contributes its value separately to the sum.

Any formula or value previously stored in address is replaced.

Returns: the evaluated sum of the new formula (using the same rules as GET, including None for cycles).


Behavioral contract

  • Invalidation: after any SET or SUM operation on a cell, all cells whose formulas depend (directly or transitively) on that cell must reflect the change on the next GET or SUM call.
  • Formula replacement: a SET on a formula cell removes the formula. A SUM on a cell that held a plain value removes that value. In both cases the old dependency edges are cleaned up.
  • Cycle behavior: a cycle is permitted to be stored; it is not rejected at write time. Evaluating any cell that lies on a cycle, or that transitively depends on one, yields None. If a subsequent update breaks the cycle, the affected cells return to normal integer values.
  • Empty refs: SUM with an empty refs list is valid; the cell evaluates to 0.

Constraints

  • 1 <= total operations <= 1000
  • Column letters are in A–Z (single character only).
  • Row numbers are integers in 1–1000.
  • Integer values passed to SET fit in a standard 32-bit signed integer.
  • Each range in refs expands to at most 26 × 1000 = 26 000 cells.
  • refs may be an empty list.
  • Cycles may be introduced at any point in the sequence.

Examples

Example 1

Input: spreadsheet_operations(['SET', 'SET', 'SUM', 'SUM', 'SET', 'GET', 'SUM', 'SET', 'GET'], [['A1', 1], ['B2', 2], ['C1', ['A1:B2', 'B2']], ['C1', ['B2']], ['C1', 5], ['C1'], ['D1', ['C1', 'A1']], ['A1', 10], ['C1']])

Output: [1, 2, 5, 2, 5, 5, 6, 10, 5]

Example 2

Input: spreadsheet_operations(['SET', 'SUM', 'GET', 'SET', 'GET'], [['A1', 3], ['B1', ['A1']], ['B1'], ['A1', 8], ['B1']])

Output: [3, 3, 3, 8, 8]

Example 3

Input: spreadsheet_operations(['GET', 'SUM', 'GET'], [['Z1000'], ['A1', []], ['A1']])

Output: [0, 0, 0]

Example 4

Input: spreadsheet_operations(['SET', 'SUM', 'SUM', 'GET'], [['A1', 4], ['B1', ['A1']], ['A1', ['B1']], ['A1']])

Output: [4, 4, None, None]

Hints

Hint 1

Keep plain values and formula definitions in separate maps. When a cell is overwritten, make sure you remove its old outgoing dependency edges before adding new ones — otherwise stale edges will corrupt future evaluations.

Hint 2

Rather than recomputing every formula cell on every write, just mark the written cell and all its transitive dependents as 'dirty' (invalidate the cache). On a GET, evaluate recursively but return the cached value immediately if the cache is still fresh.

Hint 3

To detect cycles during evaluation, thread a 'currently visiting' set through the recursion. If you arrive at a cell that is already in that set, you have found a back edge — return None and let it propagate up. Cache the None so repeat calls don't re-traverse the cycle.