Designing a Spreadsheet Engine
Difficulty: Advanced Patterns: Observer, Interpreter, Flyweight Asked at: Google, Microsoft, Figma, Notion
A grid of cells. Some hold numbers or text, some hold a formula that reads other cells, and typing into one cell has to leave every cell that reads it showing a new number before the user’s finger is off the key. Candidates expect the formula parser to be the hard part. It is the part a library would hand you. What decides whether the design works is the keystroke after the formula: which cells does this edit invalidate, and in what order do you recompute them.
Both obvious answers to that are wrong, and they are wrong in different ways. Recomputing every formula in the sheet on every edit is correct and costs O(cells) per keystroke, so a model with fifty thousand formulas does fifty thousand evaluations to show one changed number. The alternative candidates reach for – walk a cell’s dependents recursively and recompute each one as soon as it changes – is not merely slower in some cases, it is incorrect, because a cell can be reached before all of its own inputs are final. The smallest case is a diamond: A1 changes, B1 = A1+1 and C1 = A1*2 both read A1, and D1 = B1+C1 reads both. Depth-first from A1 arrives at D1 through B1 while C1 still holds its old value, so D1 is computed twice and the first of those two numbers is written into the cell where a listener can see it.
Then there is the input no design can refuse. A1 = B1 + 1 and B1 = A1 + 1 are two reasonable formulas, typed one after the other by a user who is not attacking you. Evaluation must not recurse forever, the sheet must stay usable afterwards, and the user has to be told which cells form the loop.
And a cell is mostly presentation. A hundred thousand cells in a real workbook have perhaps a dozen distinct appearances between them, so a design that gives every cell its own style object stores about a kilobyte of information in several megabytes of objects.
💡 Dependency graph = a directed graph with one node per cell and an edge from each cell to the cells its formula reads. A legal sheet is a DAG – a directed acyclic graph, meaning no path leads from a node back to itself – and a cycle in it is exactly a circular reference.
💡 Topological order = an ordering of a DAG’s nodes in which every node appears after everything it depends on. It is the order a recalculation must use, and it exists if and only if there is no cycle, which is why ordering and cycle detection are the same algorithm looked at twice.
💡 Interpreter tree, also called an AST = the formula stored as a tree of small node objects – a plus node holding a reference node and a literal – rather than as text. Parsed once per edit, walked once per recalculation. Learn more →
💡 Interning = replacing a value you just built with the one canonical instance equal to it, so a hundred thousand cells hold pointers to a dozen style objects and “same style” becomes a pointer compare. Learn more →
💡 Volatile function = one whose value can change with nothing in the sheet changing, like NOW() or RAND(). It has no inputs to invalidate it, so it cannot be cached and has to be recomputed on every pass.
Functional Requirements
- Literals – set a number, a string or a boolean into a cell addressed in A1 notation.
- Formulas – set a formula such as
=A1+B1*2,=SUM(A1:A10)or=IF(A1>0, A1, 0), parsed once into a tree that is kept on the cell. - Recalculation – editing a cell leaves every cell that transitively reads it showing a correct value, each one evaluated exactly once per edit.
- Cycle rejection – a formula that would make a cell depend on itself is refused at the moment it is typed, and the cell keeps the formula it already had.
- Range references –
SUM(A1:A100)depends on a rectangle, as one edge rather than a hundred, and reacts when any cell inside it changes. - Error values –
#DIV/0!,#VALUE!,#NAME?and#CIRCULAR!are values a cell holds, and a cell reading one is itself in error. - Change notification – views subscribe to the sheet and are told which cells changed, in one batch per edit rather than one event per cell.
Non-Functional Requirements
- Recalculation is proportional to the affected subgraph, not to the sheet – one edit costs O(affected + edges among them). Editing a cell nothing reads is O(1) regardless of how many formulas the sheet holds.
- Each affected cell is evaluated exactly once per pass – no cell observes an intermediate value of one of its inputs, and no cell is computed twice for one edit.
- Cycle handling terminates, always – detection on edit is one depth-first search bounded by the reachable subgraph, and a cycle that gets in anyway is reported as a value on every cell of the loop rather than as a stack overflow.
- Storage is proportional to populated cells – a mostly-empty sheet holds map entries for what was typed, not a dense array of nulls, and cells that look alike share one immutable style object.
Discussion
Newest first