Skip to content

How to design a spreadsheet (frontend)

The first sections are free. The deep dives, level expectations and quiz are part of Pro.

On this page
Asked at
Practice this question

The problem

A web spreadsheet shows a grid far larger than the screen, where cells feed formulas in other cells and where one paste or one undo may touch thousands of cells at once. The answer turns on touching just what matters: draw the cells in view and a little around them, recalculate the formulas downstream of an edit rather than the whole grid, and store each edit as a small reversible record rather than a copy of the whole grid. The rest of the round is showing that each of those three decisions holds up at a million cells, 100,000 formulas and 100 undo steps.

Clarifying questions

AskWhy it mattersA typical answer

How large is a large grid, and how much of it is formulas?

A grid of a few thousand cells can be drawn in full and recalculated in full. A million cells with 100,000 formulas rules both out, and that is where the design decisions start.

Plan for a million cells, 100,000 of them formulas. Most of a grid that size is empty, which matters for how you store it.

What do formulas need to support: arithmetic between cells, or range functions like a sum over a column as well?

Ranges change the dependency structure. A total over a tall column reads thousands of cells, and a naive map adds an edge for each of them.

Both. Cell references, arithmetic, and functions over ranges such as SUM. Formulas run in the browser; this is a grid one person edits.

One person editing, or several at the same time?

Several editors at once brings conflict resolution, other people's cursors and undo of changes another person has since touched. That is a different design.

One person for this round. Be ready for the follow-up about two people at once, but don't design for it up front.

Which kinds of data do columns hold, and does the column decide the type or the cell?

A type per column lets you parse input once at the column's type and give the column its own editor. A type per cell pushes that work to each cell.

Text, numbers, dates and true or false, set per column. The server exposes the column types and lets you change one with a PATCH.

Does paste come from other programs, such as Excel, or from within the grid?

Paste from outside arrives as text that has to be parsed by column type and may not fit. Paste from inside carries formulas whose references need shifting.

Both. Assume a paste of a few thousand cells copied from another spreadsheet, and that a person may then press undo.

Does the whole grid download when it opens, or can the client ask for a region?

A million cells in one response is a long wait before anything shows. A region endpoint lets the grid load what the scroll position needs.

The server answers for a region of rows and columns, so the client can load what is near the viewport and ask for more as it scrolls.

Who uses it, and with what?

Keyboard and screen reader support decide how cells are drawn, and that decision is hard to reverse later.

Mouse and keyboard, and it has to work with a screen reader. Desktop browsers first.

The requirements menu

RequirementScopeWhy

Scroll and select across a large grid of cells with the mouse and the keyboard

Core

The grid is the product. Scrolling a million cells smoothly is the first thing that breaks, so it comes first.

Edit a cell, in columns that hold text, numbers, dates or true and false

Core

Editing is why people open the grid, and typed columns decide what each cell stores and how it is shown.

Write formulas that update when the cells they use change

Core

Formulas turn a table into a spreadsheet, and dependent recalculation is the hardest correctness problem in the round.

Copy and paste a range, and undo and redo

Core

A paste in the scenario touches a few thousand cells at once, and undo has to reverse it as one step.

Freeze header rows and the first columns while the rest scrolls

Optional

Take it if there is time: it is a natural extension of the viewport math, since frozen panes are a second window drawn from the same cumulative sizes. Leave it until the main grid scrolls well.

Insert and delete rows and columns, shifting the formulas that point past them

Optional

Take it if the interviewer asks about structure changes. It touches the dependency map, the references of formulas below the change and the undo record, so it can eat the round.

Cell formatting such as bold, color and number format

Optional

Take it just as a field on the cell and a renderer that reads it. Formatting toolbars and styles add drawing work for little design signal.

Sort or filter a column

Optional

Take it if typed columns are in focus: sorting dates as dates is the clearest reason to parse once. Leave it if formulas are the deeper topic.

Several tabs in one workbook, with formulas that cross tabs

Optional

Leave it unless asked. One tab exercises everything; cross-tab references widen the address format and the dependency map's reach, which is more than a short round can carry.

Two people editing the same grid at once

Optional

Expect it as a follow-up rather than a requirement. Say how the edit records would be sent to a server and merged, and stop there.

Charts

Out of scope

A separate rendering problem. A chart reads a range much as a formula does, so the dependency map could feed it, but the grid itself is the round.

Pivot tables

Out of scope

A derived table with its own engine. It consumes the cell store but does not shape it.

Sharing and permissions

Out of scope

Server-side access control. For this round the client needs to know what it may do with the grid, and the rules behind that belong to the server.

Add-ons

Out of scope

An extension API around the grid, which assumes the grid already exists.

Non-functional requirements

RequirementWhat it forces

Scrolling stays smooth across a sheet of a million cells

The number of elements on the page has to stay tied to the screen size, not the grid size: a viewport window computed from the scroll position, positions from cached cumulative sizes, and a scroll handler that does no layout queries.

A keystroke shows within 200 milliseconds

In this design, typing goes straight into the cell editor and paints before recalculation starts. Long formula work has to leave the main thread, or the typed letter waits behind it. This is the Interaction to Next Paint threshold web.dev calls good, so it is measurable on real visits.

A formula result updates within 100 milliseconds of an edit

After one edit the engine can afford to run the formulas downstream of that cell, not all 100,000. That needs a reverse map, cell to the formulas reading it, a dirty set that holds each formula once, and a dependency order so a formula runs after its inputs.

Usable with a keyboard and a screen reader

Arrow keys move the focused cell, one tab stop for the whole grid, and the grid tells assistive technology its true row and column count even when most rows are not on the page. This weighs against painting cells on a canvas with no accessible counterpart.

Core entities

EntityWhat it holds

Cell

One addressed cell: what the person typed, what it parsed to, and the formula if there is one.

Client grid state

Everything the browser holds about the open spreadsheet.

Dependency map

Which cells each formula reads, and the reverse: who reads each cell or range.

Viewport window

The rows and columns currently on screen, plus a buffer, derived from the scroll position.

Selection

The anchor cell and the focus cell; the rectangle between them is the selected range.

Edit

One recorded change with its inverse, the unit of undo and redo.

The API

EndpointNotes

GET /sheets/{id} → { row_count, column_count, columns with types, version }

The opening call. The counts size the scrollbars and the accessible row and column counts before any cell arrives; the column types drive parsing and the per-column editors; the version is what later edits are based on.

GET /sheets/{id}/cells?rows=0-199&cols=0-49 → { cells }

Cell data by region. The client asks for the blocks around the viewport and marks them loaded, so a fast scroll to the bottom right fetches the blocks there instead of everything in between. State the assumption that the response lists occupied cells and skips empty ones, which keeps the sparse store sparse.

POST /sheets/{id}/edits { edits, base_version } → { version }

A batch of recorded edits against the version the client last saw. In this design the client applies edits locally first and sends in the background, and we'd give each edit on the wire the same change-with-inverse shape as the undo record so one type drives both. The response's meaning on a version that has moved on is not given; we'd treat it as a cue to re-fetch and reconcile.

PATCH /sheets/{id}/columns/{col} { type } → column

Changes one column's type. The client then re-parses that column once from each cell's raw text and marks the cells whose text no longer fits.

The high-level design

editcellscellsKeyboard andmouseEditorCell storeServerGrid renderer

Step 1 of 4

Store, editor and a grid that draws everything

Start with the shortest path that works: the server returns the cells, a store keyed by address holds them, an editor writes typed text into the store, and a renderer turns the store into cells on the page. It is honest about the data flow, and it already shows the problem the rest of the design answers: the renderer creates an element per cell, so a million cells means a million elements before the first scroll.

texttexteditrecordinversescrolloffsetsfetchcellseditsrangecellscountschangeddirtyvaluesKeyboard andmouseSelection andeditorClipboardPaste parserParse by columntypeColumn typesCell store byaddressUndo and redostacksViewport windowCumulative sizesServerGrid rendererAccessible gridrolesDependency mapCalc worker
The whole design

What breaks first

The deep dives

A grid of a million cells. Someone opens a spreadsheet of a million cells and drags the scrollbar straight to the bottom right corner. What does the page draw along the way, and what does it draw when the thumb stops?

A concept that helps here: List virtualizationA concept that helps here: Canvas or the DOMA concept that helps here: Accessible widgets

Formulas that depend on each other. Someone changes one input that thousands of formulas read, some of them through other formulas, and a new formula they just typed points back at itself. What runs again, in what order, and what does the self-referencing cell show?

A concept that helps here: Batching UI updates

Editing, pasting and undoing. Someone pastes a block of a few thousand cells copied from another spreadsheet, then presses undo three times. What moves where, how many times do formulas recalculate, and what does each undo restore?

A concept that helps here: Undo and redoA concept that helps here: Batching UI updates

Columns that hold different kinds of data. One column holds numbers, another dates, another true or false, and someone pastes text from another program into all three, some of it in the wrong shape. What does each cell keep, what does it show, and what do formulas that read it get?

A concept that helps here: Form validationA concept that helps here: Where state lives

Concepts this guide uses

Sources

  1. Handsontable: Row virtualization
  2. AG Grid: DOM virtualization of rows and columns
  3. techinterview.org: Build a spreadsheet, cell editor, formulas and large grids
  4. HyperFormula: Dependency graph
  5. Microsoft Learn: Excel recalculation
  6. Microsoft Support: Excel specifications and limits
  7. MDN: Using Web Workers
  8. web.dev: Web Vitals
  9. W3C ARIA Authoring Practices Guide: Grid pattern
  10. W3C ARIA Authoring Practices Guide: Grid and table properties
  11. Google Workspace Updates: Google Docs will now use canvas based rendering
  12. WebAIM: Google announces seismic change to Docs
Sign in to report a mistake