Design a Spreadsheet
📊 What is a spreadsheet engine? It is the part of a spreadsheet app that sits behind the grid. You type a number or a formula like
=A1+SUM(B1:B3)into a cell, and the engine works out its value. When you change a cell, every formula that reads it, directly or through other cells, shows the new result. If you write a formula that ends up reading itself, the engine refuses it.
This problem looks like a parsing exercise, but parsing is the easy part. What interviewers want to see is the dependency graph: which cells read which, how a change ripples through it in the right order, and how you spot a circular reference before it does any damage. It is a hard problem because a solution that works on the first example often breaks on the second one, with a diamond-shaped dependency or a formula that is overwritten.
Requirements
"Design a spreadsheet. Cells can hold numbers or formulas that refer to other cells. Values should always be up to date."
The prompt hides most of the decisions. A formula language can be anything from "two cells added together" to all of Excel, and "always up to date" can mean several different things. These questions settle it.
Clarifying Questions
You: What can a formula contain?
Interviewer: Keep it small: integers, cell references and SUM over a rectangular range, joined by plus and minus. Like =A1+SUM(B1:B3)-2. No multiplication or parentheses for now, but we may add them later.
You: What happens when a formula refers to itself, directly or through other cells?
Interviewer: Reject the edit. The call returns false and the cell keeps whatever it had before.
You: When should values update: as soon as a cell changes, or when someone reads a cell?
Interviewer: Reads are much more common than writes, since the screen shows values all the time. So reads should be cheap. Do the work when a cell changes.
You: What value does an empty cell have when a formula reads it?
Interviewer: Zero. Same for a cell that was cleared.
You: How big is the sheet, and how are cells named?
Interviewer: Columns A to Z and a given number of rows, up to a hundred. Cells are named like B7.
You: Do cells hold text, dates or decimals too?
Interviewer: Integers only. Assume formulas are well formed, so you don't need to report syntax errors.
The third answer decides the shape of the whole design. "Compute on write, read from a cache" means we must store each cell's value and keep every stored value correct after each change. That is the hard part, and it is what the rest of the post is about.
💡 Interview tip: Ask early whether the interviewer wants eager (on write) or lazy (on read) evaluation. Both are valid designs with different costs, and you should say which one you are building and why. Switching halfway through a 45-minute interview is painful.
Final Requirements
- The sheet has columns
AtoZand rows1ton. Cells are addressed by name, likeC12. - A cell is empty, holds an integer literal, or holds a formula: terms joined by
+and-, where a term is an integer, a cell reference, orSUM(X:Y)over a rectangular range. - An empty cell has value 0, including when a formula reads it.
- Setting a cell updates its value and the value of every cell that depends on it, directly or indirectly. Reading a value is cheap.
- An edit that would make a cell depend on itself is rejected: the call returns
falseand nothing changes. - The formula language will grow (more operators and functions), so adding a term type must not touch the recalculation logic.
Out of scope
- Text, dates, decimals and formatting.
- Syntax errors and error values like
#DIV/0!(we come back to error values in Extensibility). - Undo, saving to disk, and several people editing at once (also in Extensibility).
- Rendering the grid.