KatInterview

Eagerly Cached Cell Grid with Dependency Tracking

Data Structure DesignModel answer
HardModel answerData Structure Designgraphtopological-sortcycle-detection

Last updated

Build a grid of cells that stores integers or sum formulas, keeps every value precomputed for constant-time reads, and refuses any write that would introduce a circular reference.

Task

Implement a small in-memory grid of cells. Each cell holds either a plain integer or a formula that adds up other cells. Reading a cell must take O(1) time, so every cell's value has to be computed ahead of time and stored. When a write changes a cell, every cell that depends on it, directly or indirectly, must be refreshed so no read ever returns stale data.

A write that would create a circular dependency must be rejected. In that case the grid must stay exactly as it was, and the call returns False.

Interface

class Spreadsheet:
    def __init__(self):
        ...

    def set_cell(self, cell_id: str, expression: str) -> bool:
        ...

    def get_value(self, cell_id: str) -> int:
        ...

set_cell(cell_id, expression)

  • cell_id is a coordinate such as "A1" or "C3".
  • expression is one of:
    • an integer string such as "42" or "-5", or
    • a formula starting with "=" followed by cell coordinates joined by "+", such as "=A1+B2" or "=C1".
  • A reference to a cell that was never set counts as 0.
  • Returns True if the write was applied. Returns False, with no change to any state, if the write would create a cycle (including a cell referencing itself).

get_value(cell_id)

  • Returns the current stored integer for the cell, or 0 if it was never set.
  • Must run in O(1).

Examples

sheet = Spreadsheet()
sheet.set_cell("A1", "10")
sheet.set_cell("A2", "20")
sheet.set_cell("A3", "=A1+A2")
sheet.get_value("A3")   # 30

sheet.set_cell("A1", "5")
sheet.get_value("A3")   # 25
sheet = Spreadsheet()
sheet.set_cell("A1", "=B1")
sheet.set_cell("B1", "=C1")
sheet.set_cell("C1", "=A1")   # False: A1 -> B1 -> C1 -> A1 would loop
sheet.set_cell("C1", "100")
sheet.get_value("A1")         # 100

Constraints

  • Cell IDs are alphanumeric, from "A1" to "Z100".
  • Integer values lie in [-10^9, 10^9].
  • At most 2,000 calls to set_cell and 100,000 calls to get_value.
  • Formulas use only + between cell references.

What to deliver

Explain your data structures and algorithm, argue why it is correct (including the rejected-write case), give time and space complexity, and write the code.

Hints

Hint 1

Think of the grid as a directed graph where an edge goes from a cell to every cell whose formula reads it. Store both the cells each formula references and the cells that read each cell.

Hint 2

When one cell is rewritten, only the edges coming into that cell change. So you can test for a cycle on the existing graph: the new references must not already be reachable from the cell by following 'is read by' edges.

Hint 3

Do the cycle test before changing anything, so a rejection needs no rollback. Then recompute the affected cells once each, in topological order, which a DFS finishing order (reversed) gives you. Keep a list, not a set, of references so that duplicates like =A1+A1 are counted twice.