PracHub
QuestionsPremiumLearningGuidesInterview PrepNEWCoaches

Quick Overview

This question evaluates skills in data structures, expression parsing, dependency graph management, incremental evaluation, and cycle detection for maintaining consistent spreadsheet state.

  • hard
  • Harvey
  • Coding & Algorithms
  • Software Engineer

Implement Spreadsheet Cell Updates

Company: Harvey

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Technical Screen

Design and implement a small spreadsheet engine that supports setting and reading cell values. A cell name is a string such as `"A1"`, `"B12"`, or `"AA3"`. Each cell can store either an integer value or a formula string. Implement the following operations: ```text setCell(cellName, value) getCell(cellName) -> int ``` Requirements: 1. **Integer-only version** - Initially, `setCell` may receive only integer values. - `getCell` should return the current integer value of the cell. - Choose an appropriate data structure to store cell values. 2. **Formula support** - Extend `setCell` so that `value` can also be a formula string. - A formula always starts with `=`. - Formulas contain only addition using `+`. - Each term is either an integer literal or another cell reference. - There is no subtraction, multiplication, division, modulus, parentheses, or operator precedence beyond left-to-right addition. Example: ```text setCell("B1", 10) setCell("A1", "=B1+5") getCell("A1") -> 15 ``` 3. **Dependency updates** - If a cell changes, all cells that depend on it, directly or indirectly, should reflect the updated value. Example: ```text setCell("B1", 10) setCell("A1", "=B1+5") setCell("B1", 20) getCell("A1") -> 25 ``` 4. **Circular dependency detection** - Detect circular dependencies when setting a formula. - Direct and indirect cycles should both be rejected. Example: ```text setCell("A1", "=B1+1") setCell("B1", "=A1+1") // should be rejected because it creates a cycle ``` Assume undefined referenced cells have value `0`, unless you choose to explicitly reject references to undefined cells. State your choice clearly.

Quick Answer: This question evaluates skills in data structures, expression parsing, dependency graph management, incremental evaluation, and cycle detection for maintaining consistent spreadsheet state.

Part 1: Integer-Only Spreadsheet Cells

Design a tiny spreadsheet engine for integer values only. You will receive a list of operations on cell names such as A1, B12, or AA3. A SET operation stores an integer in a cell. A GET operation reads the current value of a cell. If a cell has never been assigned, its value is 0. Return the results of all GET operations in order.

Constraints

  • 0 <= len(operations) <= 100000
  • Each cell name is a non-empty string such as A1 or AA15
  • Integer values fit in 32-bit signed range
  • Undefined cells should be treated as 0

Examples

Input: [('SET', 'A1', 10), ('GET', 'A1'), ('SET', 'B2', -3), ('GET', 'B2'), ('GET', 'C1')]

Expected Output: [10, -3, 0]

Explanation: Reads back stored values, and C1 is undefined so it returns 0.

Input: [('SET', 'AA12', 7), ('GET', 'AA12')]

Expected Output: [7]

Explanation: Cell names may have multiple letters.

Input: [('SET', 'A1', 5), ('SET', 'A1', 8), ('GET', 'A1')]

Expected Output: [8]

Explanation: A later SET overwrites the previous value.

Input: []

Expected Output: []

Explanation: Edge case with no operations.

Hints

  1. A hash map keyed by cell name is enough for this version.
  2. You do not need to parse the row or column part of the cell name.

Part 2: Spreadsheet Formulas with Immediate Evaluation

Extend the spreadsheet so SET may receive either an integer or a formula string. A formula always starts with '=' and contains only addition using '+'. Each term is either an integer literal or a cell reference. In this problem, formulas are evaluated immediately when they are set, using the current values of referenced cells, and the resulting integer is stored. Later changes to referenced cells do not need to update already-stored formula results in this part. Undefined referenced cells count as 0.

Constraints

  • 0 <= len(operations) <= 100000
  • Formulas are valid and begin with '='
  • A formula uses only '+' between terms
  • Each term is either an integer literal or a cell name
  • Undefined referenced cells should be treated as 0

Examples

Input: [('SET', 'B1', 10), ('SET', 'A1', '=B1+5'), ('GET', 'A1')]

Expected Output: [15]

Explanation: A1 is evaluated immediately as 10 + 5.

Input: [('SET', 'A1', '=2+3+4'), ('GET', 'A1')]

Expected Output: [9]

Explanation: A formula may contain only integer literals.

Input: [('SET', 'A1', '=B1+7'), ('GET', 'A1')]

Expected Output: [7]

Explanation: B1 is undefined, so it contributes 0.

Input: [('SET', 'A1', 3), ('SET', 'B1', '=A1+2'), ('SET', 'C1', '=B1+4'), ('GET', 'C1')]

Expected Output: [9]

Explanation: B1 becomes 5, then C1 becomes 9.

Input: []

Expected Output: []

Explanation: Edge case with no operations.

Hints

  1. Remove the leading '=' and split the expression on '+'.
  2. For each term, decide whether it is an integer literal or a cell name.

Part 3: Live Formulas with Dependency Updates

Now formulas must stay live. A cell can store either an integer or a formula string starting with '='. Formulas use only addition, and each term is either an integer literal or another cell reference. If a referenced cell changes later, every dependent cell must reflect the new value, directly or indirectly. Undefined referenced cells count as 0. For this problem, you may assume the input never creates circular dependencies.

Constraints

  • 0 <= len(operations) <= 50000
  • Formulas are valid and begin with '='
  • A formula uses only '+' between terms
  • Each term is either an integer literal or a cell name
  • Undefined referenced cells should be treated as 0
  • No circular dependency will appear in this part

Examples

Input: [('SET', 'B1', 10), ('SET', 'A1', '=B1+5'), ('GET', 'A1'), ('SET', 'B1', 20), ('GET', 'A1')]

Expected Output: [15, 25]

Explanation: Updating B1 should also update A1.

Input: [('SET', 'A1', 1), ('SET', 'B1', '=A1+2'), ('SET', 'C1', '=B1+3'), ('GET', 'C1'), ('SET', 'A1', 10), ('GET', 'C1')]

Expected Output: [6, 15]

Explanation: The update to A1 propagates through B1 into C1.

Input: [('SET', 'X1', 1), ('SET', 'B1', '=X1+1'), ('SET', 'C1', '=X1+2'), ('SET', 'A1', '=B1+C1'), ('GET', 'A1'), ('SET', 'X1', 5), ('GET', 'A1')]

Expected Output: [5, 13]

Explanation: A1 depends on two cells that both depend on X1.

Input: [('SET', 'A1', '=B1+5'), ('GET', 'A1'), ('SET', 'B1', 10), ('GET', 'A1')]

Expected Output: [5, 15]

Explanation: Undefined references start at 0 and later updates should propagate.

Input: [('GET', 'Z9')]

Expected Output: [0]

Explanation: Edge case: reading an undefined cell returns 0.

Hints

  1. Track both forward dependencies and reverse dependencies.
  2. When one cell changes, only recompute cells reachable through the reverse-dependency graph.

Part 4: Circular Dependency Detection in a Spreadsheet

Build the full spreadsheet engine. A cell can store an integer or a live formula string starting with '='. Formulas use only addition. Each term is either an integer literal or another cell reference. If a cell changes, all dependent cells must update. In addition, if setting a formula would create a circular dependency, that SET must be rejected and the spreadsheet must remain unchanged. Detect both direct and indirect cycles. Undefined referenced cells count as 0.

Constraints

  • 0 <= len(operations) <= 50000
  • Formulas are valid and begin with '='
  • A formula uses only '+' between terms
  • Each term is either an integer literal or a cell name
  • Undefined referenced cells should be treated as 0
  • If a SET is rejected, the old cell contents and all dependent values must stay unchanged

Examples

Input: [('SET', 'A1', '=A1+1'), ('GET', 'A1')]

Expected Output: [False, 0]

Explanation: Direct self-reference is a cycle and must be rejected.

Input: [('SET', 'A1', '=B1+1'), ('SET', 'B1', '=A1+1'), ('GET', 'A1'), ('GET', 'B1')]

Expected Output: [True, False, 1, 0]

Explanation: The second SET would create a 2-node cycle, so it is rejected.

Input: [('SET', 'B1', 10), ('SET', 'A1', '=B1+5'), ('SET', 'B1', '=A1+1'), ('GET', 'B1'), ('GET', 'A1')]

Expected Output: [True, True, False, 10, 15]

Explanation: Rejected updates must not change existing values.

Input: [('SET', 'A1', '=B1+1'), ('SET', 'B1', '=C1+1'), ('SET', 'C1', '=A1+1'), ('GET', 'C1')]

Expected Output: [True, True, False, 0]

Explanation: This is an indirect 3-cell cycle, so the last SET is rejected.

Input: [('SET', 'A1', '=B1+1'), ('SET', 'A1', 7), ('SET', 'B1', '=A1+1'), ('GET', 'B1')]

Expected Output: [True, True, True, 8]

Explanation: Replacing A1 with an integer removes its dependency, so B1 can later depend on A1 safely.

Hints

  1. Adding edges from cell X to its referenced cells creates a cycle exactly when one of those referenced cells can already reach X through existing dependency edges.
  2. Do cycle detection before mutating your dependency graph.
Last updated: May 15, 2026

Loading coding console...

PracHub

Master your tech interviews with 7,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Evaluate Symbol Expressions - Harvey (medium)
  • Implement a Cursor-Based Text Editor - Harvey (medium)
  • Highlight Exact Source Matches - Harvey (medium)
  • Implement a Hierarchical File System - Harvey (medium)
  • Implement a Formula Spreadsheet - Harvey (medium)