AI Interviews · Question

"Design spreadsheet" as a long, multi-part coding problem; another report puts it at LeetCode medium or harder.

Practical coding question reported at Harvey (2025). Sources, confidence and an answer card.

Researched · updated

eng-116 AI Engineer Classic DSA & object-oriented design Practical coding Senior Medium confidence
On this page

What they’re really asking

If you only remember one thing, say this in the first minute: “Each cell holds a number or a formula that reads other cells. I’ll keep the edges both ways: what each cell reads, and who reads it. When a cell changes, I first check whether its new formula would create a loop; if so I reject the edit and change nothing. Otherwise I swap its edges and recompute it and everything downstream in dependency order, so no cell is computed from a stale value. Parsing stays separate from the graph, so ranges and functions are small additions later.”

The rest of this page explains each part of that answer.

The interviewer wants a small spreadsheet engine, built quickly and correctly, that keeps working as parts are added: values, formulas that reference cells, recompute on change, and rejecting loops.

Under the costume it is a graph problem. Each cell is a node, and “B1 reads A1” is an edge. What makes it harder than a textbook graph question is that the graph changes on every edit, and the user expects every value to be right immediately afterwards. That brings two things you must get exactly right: the order in which you recompute cells, and refusing an edit that would make a cell depend on itself.

The reports stress length and speed more than difficulty. A Harvey Senior candidate who got “Design spreadsheet” says the “question has multiple parts and follow ups”, that “You need to be really fast at coding”, and that it is “really lengthy and not doable in short time for first time”; they were rejected, with no reason given (Acedfirst-hand). A second Harvey Senior report calls its screen “at least a LeetCode medium” (Tarofirst-hand). A 2026 Harvey candidate “practiced the phone-screen problem many times. It was the Google Sheets problem, and I finished it immediately” (PracHubfirst-hand · Aug 2026). The lesson in all three is preparation and speed.

Clarify first

  • Formula language: =A1+B2*3 with + - * / only, or functions and ranges (SUM(A1:B3))? Start with arithmetic on single cells.
  • Empty cells: read as 0? Assume yes.
  • Cycles: reject the edit and leave the sheet unchanged, or store an error value in the cell? Assume reject.
  • Recompute: right away on every set (eager), or only when someone reads a cell (lazy)? Eager makes get a lookup and is the usual expectation.
  • Errors: division by zero, or a formula that reads a cell holding an error. Defer, but name them.

Say it like this: “I’ll start with plus, minus, times and divide on single cells, empty means zero, a loop rejects the edit, and I recompute eagerly. Ranges and functions later.”

Answer

Words you need first

  • A dependency graph has one node per cell and an edge for each “reads” relation. deps[C1] is what C1 reads; users[A1] is who reads A1.
  • A cycle is a loop of edges: A1 reads C1, and C1 (directly or through other cells) reads A1. No value can be computed for it.
  • A topological order lists cells so that every cell comes after everything it reads. Recomputing in that order means each cell sees fresh inputs.
  • Reverse post-order DFS is one way to get that order: do a depth-first search over users, add each cell to a list when you finish it, then reverse the list.

The shape of it

 set(cell, raw)
     │ 1. parse references from the formula
     ▼
 would any new reference reach this cell through deps?
     ├─ yes → raise CycleError; sheet unchanged
     └─ no
         │ 2. remove old edges, add new ones (deps and users)
         ▼
     3. collect the cell and everything downstream (DFS over users)
         │ 4. order them so inputs come first
         ▼
     recompute each in that order → values up to date

The steps run in exactly this order for a reason. The cycle check comes before any change, so a rejected edit leaves the old formula, the old edges and the old value. Old edges are removed before new ones are added, so a cell that stops reading A1 is no longer touched when A1 changes.

The code

On set: parse the new references; if any of them can reach this cell through deps, raise and change nothing; swap the cell’s edges; recompute the cell and everything downstream in topological order.

def set(self, cell, raw):
    new_deps = set(REF.findall(raw[1:])) if raw.startswith("=") else set()
    if self._reaches(new_deps, cell):              # would close a cycle: reject, change nothing
        raise CycleError(cell)
    for d in self.deps[cell]:
        self.users[d].discard(cell)
    self.raw[cell], self.deps[cell] = raw, new_deps
    for d in new_deps:
        self.users[d].add(cell)
    for c in self._affected(cell):                 # cell first, then dependents in topo order
        self.value[c] = self._eval(c)

def _affected(self, cell):                         # reverse post-order DFS over users
    order, seen = [], set()
    def visit(c):
        seen.add(c)
        for u in self.users[c]:
            if u not in seen:
                visit(u)
        order.append(c)
    visit(cell)
    return order[::-1]

One sheet through it

Start with A1 = 10, B1 = =A1*2, C1 = =A1+B1. So B1 is 20 and C1 is 30. C1 reads A1 twice: once directly and once through B1. That shape (two paths to one cell) is called a diamond, and it is where naive code breaks.

EditWhat the engine doesResultWhat to say in the interview
set(A1, "5")finds A1, B1, C1 downstream; orders them A1, B1, C1B1 = 10, C1 = 15”C1 must come after B1, because it reads B1.”
Same edit, but recomputing in plain breadth-first (BFS) orderfrom A1 it may visit C1 before B1C1 = 5 + 20 = 25, wrong”This is why breadth-first recompute is a bug: C1 read the old B1.”
set(A1, "=C1")C1 reaches A1 through deps: a cyclerejected; A1 stays 5”Check first, change nothing on failure.”
set(C1, "7")removes C1 from the readers of A1 and B1C1 = 7; a later edit to A1 no longer touches C1”Old edges must go, or every edit to A1 would still recompute C1 for nothing.”

The tests to write: a chain of three cells updates after one edit, a rejected cycle leaves the old value and the old formula, removing a reference drops the edge (C1 stops changing when A1 does), and a self-reference is a cycle. All four passed when run on 2026-10-12. The family walkthrough shows the same staged approach on a credit ledger.

Likely next parts

  • Ranges: expand A1:B3 into its cells while parsing, so the graph code doesn’t change at all.
  • Error values that spread to every cell that reads an error cell.
  • Undo: store the previous raw value for each edit and set it back.
  • Lazy recompute for large sheets, where one edit could otherwise touch thousands of cells.

Say it like this: “Ranges are a parser change, not a graph change. That’s why I kept them apart.”

Senior vs Staff

Spreadsheet

Senior

  • Correct dependency graph with both edge directions, atomic cycle rejection, topological recompute, tests per part.

Staff

  • Says why BFS recompute is wrong, keeps parsing separate from the graph so ranges and functions are small changes, and discusses eager versus lazy recompute and the cost of a cell with thousands of dependents.

Small-team version

Nobody at a small company writes a spreadsheet engine, but the shape returns as any graph of derived values. One example in AI work is a search pipeline: when a document changes, you must re-split it into chunks, recompute each chunk’s embedding (a list of numbers representing its meaning, produced by a model call that costs money), and update the search index, but only for what depends on that document. Another is prompt templates built from other templates.

The habits carry over directly: store edges both ways, reject cycles at write time, and recompute in topological order so nothing is computed twice or from stale inputs. For job graphs, use an engine that already does this rather than writing one.

Pitfalls

  • Recomputing in breadth-first order. On a diamond, a cell can be computed from a stale input.
  • Detecting the cycle after changing the graph. A rejected edit then leaves corrupted edges behind.
  • Forgetting to remove old edges when a formula changes. Cells keep recomputing for inputs they no longer read.
  • Recursive evaluation without memoization. Each cell gets recomputed once per path, which grows exponentially on stacked diamonds.
  • Trying it for the first time in the round. Candidates report this problem is long; practise it before.

Go deeper

Family