Evaluate Spreadsheet Cells That Reference Other Cells

Last updated: November 29, 2026

Quick Overview

Each cell holds either an integer or a formula that is the sum of other cells, like '=A1+B2+5'. Given a mapping of cell names to their contents, return the numeric value of every cell, or report the cells involved in a circular reference as errors.

Meta
Coding & Algorithms
Frontend Engineer
Meta
November 29, 2026
Frontend Engineer
Onsite
Coding & Algorithms
Medium

0

0

0 solved


Each cell holds either an integer or a formula that is the sum of other cells, like '=A1+B2+5'. Given a mapping of cell names to their contents, return the numeric value of every cell, or report the cells involved in a circular reference as errors.

A Meta onsite question in the style of building a lightweight spreadsheet inside a web tool such as an internal dashboard editor. The interviewer checks parsing, building a dependency graph from formulas, evaluating in topological order, and marking every cell that cannot be resolved because of a cycle.

What the Interviewer Expects
  • Parse formulas into referenced cells and a constant term
  • Build edges from each referenced cell to the formula cell
  • Evaluate in topological order with Kahn's algorithm
  • Mark cells on or downstream of a cycle as errors
  • Treat references to empty cells as zero (after clarifying)
Key Topics to Cover
topological sort
expression parsing
cycle detection
dependency graph
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 recompute only affected cells when one cell is edited?
  • How would you support functions like SUM(A1:A10) with ranges?
  • How do you distinguish cells on a cycle from cells that merely depend on one?
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
  • Input: cells, a dict like {"A1": "5", "B1": "=A1+3", "C1": "=B1+A1"}.
  • Formulas are = followed by +-separated terms, each a cell name or integer.
  • Output: dict of cell to integer value, wi...
High-Level Design
  1. Parse. For each formula split on +; numeric terms add to a constant, others are references.
  2. Graph. Edge ref -> cell for each reference to a defined cell. In-degree of a cell is its n...

Submit Your Answer
Markdown supported