Evaluate Spreadsheet Cell Formulas With Cycle Detection
Last updated: November 29, 2026
Quick Overview
Given a map from cell names to contents, where a cell is either an integer or a formula summing other cells and constants (like "=A1+B2+3"), compute every cell's value and mark cells that are in or depend on a circular reference as an error.
Microsoft
November 29, 20260
0
0 solved
Given a map from cell names to contents, where a cell is either an integer or a formula summing other cells and constants (like "=A1+B2+3"), compute every cell's value and mark cells that are in or depend on a circular reference as an error.
An Excel-flavored graph problem used in Microsoft onsites to see whether you can model cells as a dependency graph, memoize evaluation, and detect cycles in the same traversal. Strong candidates also discuss incremental recalculation, which is what Excel actually does when one cell changes.
What the Interviewer Expects
- Models references as directed edges and evaluates with memoized DFS
- Detects cycles using an in-progress set (gray nodes) rather than a separate pass
- Propagates the error to every cell that depends on a cycle
- Treats references to empty cells sensibly and parses terms robustly
- Discusses recursion depth and an iterative or topological alternative
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- When one cell changes, how do you recompute only its dependents, in the right order?
- How would you support ranges like SUM(A1:A100) without creating 100 edges per formula?
- How would you handle very long dependency chains that overflow the call stack?
- How would you parallelize recalculation across a large workbook?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Cells are named like A1. Content is an integer or a formula = followed by terms joined with +; each term is a cell reference or an integer. Referencing an empty cell yields 0. A cell on a cycle,...
High-Level Design
Each formula creates edges from the cell to the cells it reads. Evaluating a cell is a DFS: evaluate dependencies, then combine. Memoizing results makes the whole sheet O(V + E). For cycles I use the ...