Recalculate Dependent Spreadsheet Cells After Each Edit

Last updated: November 29, 2026

Quick Overview

Implement a small Google Sheets style engine where each cell holds an integer or a formula like '=A1+B2+3' (a sum of cell references and integers). set(cell, text) updates a cell and recalculates every cell that depends on it, rejecting the edit if it would create a circular reference; get(cell) returns the current value.

Google
Coding & Algorithms
Software Engineer
Google
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Hard

0

0

0 solved


Implement a small Google Sheets style engine where each cell holds an integer or a formula like '=A1+B2+3' (a sum of cell references and integers). set(cell, text) updates a cell and recalculates every cell that depends on it, rejecting the edit if it would create a circular reference; get(cell) returns the current value.

A Google onsite question that sits between simulation and graph algorithms. The interviewer checks that you maintain a reverse dependency graph, recompute only affected cells in topological order, and detect cycles before mutating state.

What the Interviewer Expects
  • Parse formulas into referenced cells plus a constant
  • Maintain a dependents (reverse) graph for incremental recalculation
  • Detect cycles by searching from the edited cell through dependents to its new references
  • Recompute affected cells in topological order exactly once each
  • Leave state unchanged when an edit is rejected
Key Topics to Cover
dependency graph
topological sort
cycle detection
incremental computation
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • How would you support functions like SUM over a range such as A1:A100?
  • How would you parallelize recalculation for large sheets?
  • How would collaborative edits from two users interact with recalculation?
  • How would you show #REF or #CYCLE errors instead of rejecting?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

Cells are named like A1. Empty cells evaluate to 0. set(c, '5') stores a constant; set(c, '=A1+7+B2') stores a formula. set returns True on success, False if the new references would form a cy...

High-Level Design

Store formula[c] = (refs, const), value[c], and dependents[r] = set of cells whose formula references r.

On set(c, text): parse. Cycle check: DFS from c through dependents; if any new ref...


Submit Your Answer
Markdown supported