KatInterview

Build a Dependency-Aware Cell Calculator with Cycle Checks

Data Structure DesignModel answer
MediumModel answerData Structure Designdesigngraphcycle-detection

Last updated

Design an in-memory grid of numeric and formula cells in Python, reject circular dependencies, allow shared references, and compare lazy evaluation against eager recomputation with caching.

Build a small in-memory grid, similar to a spreadsheet, as a Python class. It exposes three operations:

  • set_cell(cell, value): store a plain number in the named cell.
  • set_formula(cell, formula): store a formula string in the named cell. Formulas use cell names, numeric literals and the operators +, -, *, /.
  • get_cell(cell): return the current numeric value of the cell.
set_cell("A1", 5)
set_cell("A2", 3)
set_formula("B1", "A1 + A2")
set_formula("C1", "B1 * 2")     # C1 depends on another formula cell
get_cell("C1")  -> 16
set_cell("A1", 10)
get_cell("C1")  -> 26
set_formula("A1", "C1 - 1")
get_cell("A1")  -> error: circular reference A1 -> C1 -> B1 -> A1

Your design may instead reject the last set_formula call itself, if you check for cycles when a formula is written. State which you chose.

Before you code

Decide and state your assumptions on:

  • Formula format: a string needing precedence and parentheses, or a pre-parsed structure? Are numeric literals allowed?
  • What a reference to a never-set cell evaluates to (zero or an error).
  • How division by zero is reported.
  • Whether cycles are reported at write time or at read time.
  • Whether set_cell replaces an existing formula in that cell and vice versa.

Part 1: Core operations

Implement the three methods for formulas whose operands are numbers and cells holding plain numbers. Cover precedence of the four operators, division by zero, malformed formulas, and overwriting a cell. Do not use eval on user input.

Part 2: Nesting, cycles and repeated references

Formulas may reference other formula cells to any depth. Detect and raise an error on circular references, both direct (A1 = A1 + 1) and indirect (A1 -> B1 -> C1 -> A1), and report the cycle. References that repeat without forming a cycle must keep working: a formula naming the same cell twice, or two cells reading one shared cell. A shared cell should not be recomputed needlessly within one read, and the spreadsheet should stay consistent after an error.

Part 3: Complexity and trade-offs

Analyze the cost of your design. Why compute values inside get_cell? Could the work move into the set operations? If writes are rare and reads frequent, which design fits better and what extra structures does it need?

Hints

Hint 1

Parse each formula once when it is set and keep only the list of referenced cell names plus an evaluable form (for example a small expression tree or reverse-Polish tokens). Write a tiny recursive-descent or shunting-yard parser instead of using eval.

Hint 2

For cycle detection use a depth-first evaluation with three states per cell: unvisited, in progress (on the current path), and done. Meeting an in-progress cell means a cycle; meeting a done cell is just a shared reference and must be reused, not flagged. Keep the path as a list so you can print A1 -> C1 -> B1 -> A1.

Hint 3

For the trade-off discussion, keep a forward map (cell to the cells it reads) and a reverse map (cell to the cells that read it). On any write, remove the cell's old edges from the reverse map before adding new ones, then invalidate cached values for everything reachable through the reverse map.