KatInterview

In-Memory Grid with Formula Evaluation and Cycle Guards

Data Structure DesignModel answer
MediumModel answerData Structure Designgraphsdependency-trackingparsing

Last updated

Build a spreadsheet class in three stages: plain integer cells, formula parsing with dependency-order evaluation, then cycle detection and targeted recomputation.

In-Memory Grid with Formula Evaluation and Cycle Guards

You are building a lightweight in-memory grid that supports both plain values and computed cells. Implement it in three stages, finishing and verifying each one before moving on — the full scope is substantial for a single interview, and a working Part 1 is worth more than a half-finished Part 3.

Cells are identified by a label: one uppercase letter followed by one or more digits (e.g. A1, C12). The class exposes exactly two methods:

class Grid:
    def set_cell(self, label: str, value) -> None: ...
    def get_cell(self, label: str) -> int | None: ...

Clarifying questions worth raising before you start

  • Single-letter columns only, or also multi-letter ones like AA3?
  • Is a1 the same cell as A1, or are labels case-sensitive?
  • Can a cell be overwritten with a different type (number → formula or vice versa)?
  • Can values or formula results be negative?

Part 1 — Plain integer cells

set_cell(label, value) stores an integer. get_cell(label) returns it. No formulas yet.

Cover:

  • A keyed storage structure and a clear return value for a label that has never been set.
  • Basic label validation if the interviewer asks for it.
  • Speed: this part should take only a few minutes.

Part 2 — Formula cells

set_cell now also accepts a formula string beginning with =, for example:

  • set_cell("A1", "=B1+C1+1") — sum of two cells and a constant
  • set_cell("A1", "=1+2") — formula with no cell references

get_cell always returns a pre-computed integer (O(1)). Recomputing every stored cell on each write is acceptable here. No circular references will occur in this stage.

Clarifying questions for this part:

  • Which operators are supported — just +, or also -, *, and parentheses?
  • If a formula references a cell that has never been set, is that cell treated as 0 or does the formula itself become None/an error?
  • Can formulas contain whitespace? How should a malformed formula be reported?

Cover:

  • Parsing a formula string into cell references and integer literals.
  • Evaluating cells in a correct dependency order so that chains like A1 = B1 + 1, B1 = C1 + 1 produce the right answer.
  • A single consistent rule for never-set cells (both direct reads and formula references).
  • An honest discussion of the write cost of recomputing everything.

Part 3 — Cycle detection and selective recomputation

Formulas may now form cycles, e.g. set_cell("A1", "=B1") then set_cell("B1", "=A1+1"). Your implementation must:

  1. Detect cycles before committing any state change, including self-references like set_cell("A1", "=A1+1").
  2. Recompute only affected cells — those that transitively depend on the cell that just changed — rather than the whole grid.

Clarifying questions for this part:

  • Should a cycle-creating call be rejected (sheet unchanged) or accepted with the involved cells marked as errors?
  • Is a self-reference a cycle?

Cover:

  • A forward-dependency and reverse-dependency graph that stays consistent when a formula is replaced or a cell is changed back to a plain number.
  • A traversal that finds the topological evaluation order for just the affected subgraph.
  • How set_cell and get_cell complexity changes from Part 2 to Part 3.

Hints

Hint 1

For Part 2, store three things per cell: the raw input, the set of cell labels it references, and the last computed value. That structure extends cleanly into Part 3 without a rewrite.

Hint 2

Correct evaluation order in Part 2 and Part 3 is a topological sort of the dependency graph. In Part 2 you can run it over all cells; in Part 3 run it only over the transitive dependents of the changed cell (found by following reverse-dependency edges).

Hint 3

Cycle detection in Part 3 is a DFS from the newly referenced cells: if you can reach the cell being written before finishing the DFS, the write would form a cycle and should be rejected before any state is mutated.