Implement a Formula Spreadsheet

Quick Overview

This question evaluates expression parsing, symbolic cell reference resolution, dependency graph construction, cycle detection, and stateful API design for an in-memory spreadsheet.

Implement a Formula Spreadsheet

Company: Harvey

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

Design and implement a small in-memory spreadsheet. The spreadsheet must support cell labels such as `A1` and `B10`, where a label consists of uppercase letters followed by a positive row number. Implement the following API: ```python set_cell(label, value) get_cell(label) -> int ``` ### Part 1: Plain integer cells A cell can store an integer value. Example: ```python set_cell("A1", 10) get_cell("A1") # returns 10 ``` ### Part 2: Formulas A cell can also store a formula. A formula is an expression containing only: - integer literals, - cell references, - the `+` operator. You do not need to support subtraction, multiplication, division, parentheses, or operator precedence beyond addition. Examples: ```python set_cell("A1", "10") set_cell("A2", "A1+20") get_cell("A2") # returns 30 set_cell("A3", "A1+A2+5") get_cell("A3") # returns 45 ``` Formulas should be evaluated using the current values of referenced cells. If a referenced cell does not exist, raise an error. ### Part 3: Cycle detection The spreadsheet must detect cyclic dependencies and report an error instead of recursing forever. Example: ```python set_cell("A1", "B1+1") set_cell("B1", "A1+1") get_cell("A1") # raises a cycle-detected error ``` Implement the data structures and logic needed for `set_cell` and `get_cell`.

Quick Answer: This question evaluates expression parsing, symbolic cell reference resolution, dependency graph construction, cycle detection, and stateful API design for an in-memory spreadsheet.

|Home/Coding & Algorithms/Harvey
Harvey logo
Harvey
Apr 11, 2026, 12:00 AM
mediumSoftware EngineerTechnical ScreenCoding & Algorithms
2
0

Design and implement a small in-memory spreadsheet.

The spreadsheet must support cell labels such as A1 and B10, where a label consists of uppercase letters followed by a positive row number.

Implement the following API:

set_cell(label, value)
get_cell(label) -> int

Part 1: Plain integer cells

A cell can store an integer value.

Example:

set_cell("A1", 10)
get_cell("A1")  # returns 10

Part 2: Formulas

A cell can also store a formula. A formula is an expression containing only:

  • integer literals,
  • cell references,
  • the + operator.

You do not need to support subtraction, multiplication, division, parentheses, or operator precedence beyond addition.

Examples:

set_cell("A1", "10")
set_cell("A2", "A1+20")
get_cell("A2")  # returns 30

set_cell("A3", "A1+A2+5")
get_cell("A3")  # returns 45

Formulas should be evaluated using the current values of referenced cells. If a referenced cell does not exist, raise an error.

Part 3: Cycle detection

The spreadsheet must detect cyclic dependencies and report an error instead of recursing forever.

Example:

set_cell("A1", "B1+1")
set_cell("B1", "A1+1")
get_cell("A1")  # raises a cycle-detected error

Implement the data structures and logic needed for set_cell and get_cell.

Submit Your Answer to Earn 20XP

Sign in to leave a comment

Loading comments...