Design and implement a tiny Spreadsheet engine.
- Each cell is addressed by an ID such as
A1,B1, … - A cell can store either:
- an integer literal, e.g.,
42, or - a formula of the form
X + Y, whereXandYare cell IDs (no minus/multiply in the base version).
- an integer literal, e.g.,
- Implement:
setCell(id, valueOrFormula)— set a cell to an integer or to a formula string like"A1 + B1".getCellValue(id)— return the evaluated integer value of the cell.
Rules / constraints
- A formula’s value is the sum of its referenced cells.
- Cells may depend on other cells transitively (
A1 = B1 + C1,B1 = D1 + 3, etc.). - You may assume inputs are valid in Part 1 (no cycles); a simple DFS that follows dependencies is acceptable.
Follow-ups
- There will be many queries per second (
getCellValuecalled frequently). Optimize beyond naïve DFS. - Allow updates to cells. When a cell changes, affected dependent cells should be recomputed efficiently.
- (Optional) Detect cycles and report an error.
- (Optional) Extend formula grammar to allow integer literals inside expressions (
A1 + 3) and more operators.
Example
setCell("A1", 3)
setCell("B1", 5)
setCell("C1", "A1 + B1")
getCellValue("C1") → 8
setCell("B1", "A1 + 1") # C1 depends on B1 which depends on A1
getCellValue("C1") → 7 # (A1=3, B1=4, C1=7)
- Graph model:
deps[cell] = [u, v](operands);parents[child]for reverse edges. - Evaluation:
def eval(x): if x in memo: return memo[x] if is_int(x): return x a, b = deps[x] memo[x] = eval(a) + eval(b) return memo[x] - Update (
setCell):- remove old edges, add new edges;
- BFS/DFS from
idviaparentsto invalidate memo of all dependents.
This mirrors the interview: start with simple DFS, then show scalability via caching + dependency invalidation, and optionally cycle detection.
The VOprep team has long accompanied candidates through various major company OAs and VOs, including OpenAI, Google, Amazon, Citadel, SIG, providing real-time voice assistance, remote practice, and interview pacing reminders to help you stay smooth during critical moments. If you are preparing for Stripe or similar engineering-focused companies, you can check out our customized support plans—from coding interviews to system design, we offer full guidance to help you succeed.