Design the front end of a collaborative spreadsheet like Google Sheets.
Users open a workbook with one or more sheets, scroll through large grids, type values and formulas (=SUM(A1:A100),
=VLOOKUP(...)), format cells, resize rows and columns, freeze panes, and see colleagues' selections and edits live.
The interviewer adds:
Focus on grid rendering, the formula engine and dependency graph, where computation runs (client vs server), the collaboration model, and performance. Assume you can define the backend APIs.
A spreadsheet looks like "a big table", but the interesting parts are a rendering problem (a grid far larger than any DOM can hold), a compute problem (a reactive formula engine with a dependency graph) and a collaboration problem (concurrent edits where the unit of change is a cell or a structural operation such as "insert row"). Say this framing early: it organises the whole answer.
Tab, Enter, Ctrl+Arrow to jump to
data edges, Shift to extend).A1, $A$1, A1:B10, Sheet2!C3, whole-column A:A), ~100 common functions.| Concern | Target |
|---|---|
| Scrolling | 60 fps on a 1M-row sheet; no blank flashes during fast scroll on a mid-range laptop |
| Edit latency | typed value visible immediately; dependent formulas updated < 100 ms for ≤ 10k dependents |
| Load | first meaningful grid paint < 1.5 s for a 50k-cell sheet; large sheets load progressively |
| Memory | sparse storage: memory proportional to non-empty cells, not rows × cols |
| Collaboration | remote edits visible p95 < 500 ms; all clients converge on identical values |
| Correctness | client and server compute identical results (same function semantics, float handling) |
| Accessibility | APG grid semantics for screen readers despite canvas rendering |
NOW(), RAND()?IMPORTRANGE, API-backed functions) that must run server-side?Assume: most sheets < 50k non-empty cells, cap of 10M cells; formulas compute client-side for responsiveness with the server also able to compute (for headless exports, API reads and very large sheets).
<canvas> and use DOM only for overlays: the cell editor, comments, dropdowns,
and an accessibility layer. DOM virtualization is the right answer for a 20-column admin table; canvas is the
right answer for a 100-column spreadsheet with arbitrary formatting.| Component | Responsibility |
|---|---|
| Shell (React) | toolbar, formula bar, sheet tabs, menus, dialogs, sharing |
| Grid renderer | canvas layers: cells, gridlines, headers, selection; computes visible window from scroll offset |
| Geometry index | row heights / column widths with prefix sums for O(log n) offset ↔ index lookup |
| Interaction controller | pointer, keyboard, selection model, fill handle, resize handles, cell editor overlay |
| Sheet store | sparse cell map, formats, structure (row/col metadata); main-thread copy for rendering |
| Calc worker | parser, dependency graph, evaluator, function library; posts changed values back |
| Sync client | WebSocket, op queue (pending/in-flight), transform of local ops against remote ops |
| Presence | collaborators' selections and active cell |
| A11y layer | off-screen DOM role="grid" window around the active cell, live announcements |
=SUM(A1:A100) in C1 and presses Enter.setCell(sheet, C1, {formula}), applies it optimistically to the store, and
queues it for sync.{cell: value} patches.type SheetId = string;
type CellKey = number; // row * MAX_COLS + col, a single number is cheaper than "A1" strings
interface Cell {
input?: string; // what the user typed: "42", "hello", "=A1*2"
value?: CellValue; // computed or literal value
formatId?: number; // index into a deduplicated format table
}
type CellValue =
| { t: 'n'; v: number }
| { t: 's'; v: string }
| { t: 'b'; v: boolean }
| { t: 'e'; v: '#REF!' | '#DIV/0!' | '#VALUE!' | '#NAME?' | '#N/A' | '#CYCLE!' };
interface Sheet {
id: SheetId;
name: string;
rowCount: number; // logical size, e.g. 1_000_000
colCount: number;
cells: Map<CellKey, Cell>; // only non-empty cells
rowMeta: Map<number, { height?: number; hidden?: boolean }>;
colMeta: Map<number, { width?: number; hidden?: boolean }>;
merges: Range[];
frozen: { rows: number; cols: number };
}
interface Range { r1: number; c1: number; r2: number; c2: number; sheet?: SheetId }
Formats are deduplicated into a table (formats[formatId]), because a sheet with 50k bold red cells should store one
format object, not 50k. Column-oriented typed storage (Float64Array per column chunk) is an optimisation for very
large numeric sheets.
Structural edits need a stable mapping: if rows are identified purely by index, inserting a row means re-keying every cell below. Two options: (a) accept the re-key cost in the store (fine for 50k cells) or (b) give rows/cols stable IDs and keep an index→ID ordered list (more complex, but makes concurrent structural edits and references cheaper to rebase). Mention the trade-off; choose (b) for the collaborative engine, translating to indices at the boundary.
interface FormulaNode {
key: CellKey;
ast: Expr;
precedents: Array<CellKey | Range>; // what this formula reads
volatile: boolean; // NOW(), RAND()
}
interface DepGraph {
formulas: Map<CellKey, FormulaNode>;
// reverse edges: who depends on a given cell
dependents: Map<CellKey, Set<CellKey>>;
// range dependencies stored separately in an interval/R-tree style index,
// so A1:A100000 is one entry, not 100k edges
rangeDeps: RangeIndex<CellKey>;
}
Selection (anchor, focus, extra ranges), scroll offsets, editing state, clipboard snapshot: local, not synced.
Presence: ephemeral via the socket.
GET /api/workbooks/:id -> { id, title, sheets: [{ id, name, rowCount, colCount }], role, version }
GET /api/workbooks/:id/sheets/:sid/cells?r1=0&r2=999&c1=0&c2=50
-> { version, cells: [[row, col, input, value, formatId]], formats: {...} }
GET /api/workbooks/:id/snapshot?version=1234 -> binary snapshot for fast open
Large sheets load in row-band chunks (for example 1,000 rows), prioritising the viewport, then background-filling the rest so formulas over full columns can compute. For sheets beyond the client cap, the server computes and the client only fetches windows of values (a "thin client" mode).
type Op =
| { type: 'setCells'; sheet: SheetId; cells: Array<{ r: number; c: number; input: string | null }> }
| { type: 'setFormat'; sheet: SheetId; range: Range; formatPatch: Partial<Format> }
| { type: 'insertRows'; sheet: SheetId; at: number; count: number }
| { type: 'deleteRows'; sheet: SheetId; at: number; count: number }
| { type: 'insertCols' | 'deleteCols'; sheet: SheetId; at: number; count: number }
| { type: 'resize'; sheet: SheetId; axis: 'row' | 'col'; index: number; size: number }
| { type: 'addSheet' | 'renameSheet' | 'deleteSheet'; sheet: SheetId; name?: string };
// client -> server
type Submit = { t: 'submit'; baseVersion: number; clientOpId: string; ops: Op[] };
// server -> client
type Commit = { t: 'commit'; version: number; clientOpId?: string; authorId: string; ops: Op[] };
type Presence = { t: 'presence'; userId: string; sheet: SheetId; selection: Range[] };
Only inputs are synced, never computed values: every client recomputes deterministically. This keeps messages small and avoids "who computed what" conflicts, at the price of requiring identical function semantics across client and server engines (share the engine code, compiled for both).
type ToWorker =
| { t: 'load'; sheets: SerializedSheet[] }
| { t: 'apply'; ops: Op[]; txId: number }
| { t: 'tick' }; // for volatile functions
type FromWorker =
| { t: 'values'; txId: number; patches: Array<[SheetId, CellKey, CellValue]> }
| { t: 'progress'; txId: number; done: number; total: number };
scrollTop, binary-search the row prefix-sum array to find the first visible row; same
for columns. Only draw that window (typically ~50 rows × ~20 columns, ~1,000 cells).cssSize * devicePixelRatio and scale the context, otherwise
text is blurry. Align 1 px gridlines to half-pixels.measureText is expensive; cache widths per (font, string) for overflow and
auto-fit calculations.OffscreenCanvas in a worker is possible, but the main thread still needs geometry for
hit testing. Most teams keep drawing on the main thread (it is fast when bounded to the window) and move
compute off-thread.#CYCLE! (or support iterative calculation as a setting, like Excel).SUM(A:A) must not create a million edges. Store range precedents in an interval index
keyed by column; when a cell changes, query "which ranges contain (r, c)" in O(log n + k).NOW() heavy sheets are slow
in every spreadsheet product.=A5 to =A6 in every formula. With stable row IDs,
ASTs reference IDs and only display text changes.0.1 + 0.2 = 0.30000000000000004; spreadsheets round displayed values to 15
significant digits. Dates are serial numbers; locales change decimal and argument separators (; in many
European locales).setCells on the same cell: last committed wins (server order). Show a brief highlight on the cell so
the overwritten user notices.setCells at row 10 concurrent with insertRows(at: 5): transform the cell op to row 11. deleteRows covering a
cell being edited: the edit is dropped and the editor overlay closes with a toast.#REF! deterministically on all clients.Y.Map per cell, Y.Array of row IDs), and it gives offline merge. But formulas
referencing ranges, merges and row moves need validation and a canonical order anyway, so the server sequencer is
simpler. If offline-first is a hard requirement, a CRDT of row/column ID arrays plus LWW cell registers works.text/plain (TSV) and text/html (a <table> with inline styles) to the clipboard so Excel and Docs
understand it. Internally, also store a richer payload (formulas with relative references) for in-app paste.mso- styles; parse into values and a subset of formats. Sanitise.Canvas content is invisible to assistive technology, so:
role="grid", aria-rowcount/aria-colcount with the full logical size, rows with
aria-rowindex, cells with aria-colindex) containing only the active cell's neighbourhood, positioned off-screen
or transparently over the canvas.aria-activedescendant (or roving focus) points at the active cell. Announce value,
formula and coordinates ("C1, formula SUM A1 to A100, value 5,050").SharedArrayBuffer (requires cross-origin isolation headers — a deployment cost to call out).How do you implement fill-down (drag the fill handle)? Generate setCells for the target range, offsetting
relative references in each formula AST and detecting series (1, 2, 3 or Mon, Tue). It is one op batch and one undo
step.
How do you handle a formula referencing another workbook? Treat it as an external, asynchronous dependency:
the server resolves it with the user's permissions and pushes values; the cell shows Loading… then the value.
Why sync inputs rather than values? Values are derived; syncing them doubles traffic and creates conflicts between computed results. Syncing inputs and computing deterministically guarantees convergence if the engine is deterministic.
What breaks determinism? Volatile functions (RAND, NOW), locale-dependent parsing, and floating point
differences between engines. Seed or server-assign volatile results when they must match, and parse in a canonical
locale.
How would you support conditional formatting on 1M rows? Evaluate rules lazily for visible cells only, cache per cell with invalidation on dependency change, and treat rules like formulas in the dependency graph.
Could you use a DOM grid with virtualization instead? For a few thousand rows and modest formatting, yes, and it gives a11y for free. It breaks down with many columns, merged cells, text overflow and 60 fps scroll, which is why large sheet products moved to canvas.
Spotted a mistake or a rights concern? Report an issue