Evaluate a Spreadsheet of Formulas With Cycle Detection

Last updated: November 29, 2026

Quick Overview

Given a map from cell names to raw contents (numbers or formulas like `=A2+B1*2` with parentheses and unary minus), compute every cell's value, marking cells in or depending on a reference cycle as `#CYCLE` and cells referencing missing cells as `#REF`.

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

0

0

0 solved


Given a map from cell names to raw contents (numbers or formulas like `=A2+B1*2` with parentheses and unary minus), compute every cell's value, marking cells in or depending on a reference cycle as `#CYCLE` and cells referencing missing cells as `#REF`.

Spreadsheet evaluation is a classic OpenAI onsite coding problem because it combines a small expression parser with dependency graph traversal and forces you to make error semantics explicit. Interviewers look for clean separation between parsing and evaluation, memoization so each cell is computed once, and correct propagation of errors through dependents.

What the Interviewer Expects
  • Write a correct precedence-aware parser (recursive descent or shunting-yard)
  • Memoize so each cell is evaluated once
  • Detect cycles with visiting and done states during DFS
  • Propagate errors to every dependent cell
  • Explain the time complexity in terms of cells and formula length
  • Keep the code modular enough to extend with functions like SUM
Key Topics to Cover
recursive descent parsing
DFS cycle detection
memoization
dependency graphs
error propagation
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
  • Support `SUM(A1:B3)` ranges: what changes in parsing and dependency tracking?
  • A user edits one cell: how do you recompute only affected cells?
  • How would you report which cells form the cycle rather than just flagging them?
  • Formulas form a dependency chain of depth 100,000: how do you avoid recursion limits?
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
  • Raw cell: a number, or = followed by an expression of numbers, cell refs, + - * /, parentheses and unary minus.
  • Output: number for each cell, or #CYCLE / #REF.
  • A cell that depends (direc...
High-Level Design

Evaluate lazily with DFS. value_of(cell) returns a memoized value, or marks the cell as visiting, parses and evaluates its formula (recursively calling value_of for references), then marks it done...


Submit Your Answer
Markdown supported