Implement a Spreadsheet With Formulas
Company: Harvey
Role: Machine Learning Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates understanding of data structures and algorithms for dependency management, expression parsing and evaluation, and detection of circular references in a spreadsheet-style system.
Part 1: Basic Spreadsheet Cells
Constraints
- 0 <= len(operations) <= 100000
- Each cell_id consists of one or more uppercase letters followed by one or more digits
- Values fit in a signed 32-bit integer
- All operations are valid and are either SET or GET
Examples
Input: [['SET', 'A1', '10'], ['GET', 'A1'], ['SET', 'B2', '7'], ['GET', 'B2']]
Expected Output: [10, 7]
Explanation: Basic set and get operations on two different cells.
Input: [['GET', 'C3'], ['SET', 'C3', '4'], ['GET', 'C3']]
Expected Output: [0, 4]
Explanation: An unset cell should read as 0.
Input: [['SET', 'AA10', '-3'], ['GET', 'AA10'], ['SET', 'AA10', '8'], ['GET', 'AA10']]
Expected Output: [-3, 8]
Explanation: Handles multi-letter column names, negative values, and overwriting an existing cell.
Input: []
Expected Output: []
Explanation: Edge case: no operations means no outputs.
Hints
- A hash map from cell ID to integer value is enough for this part.
- When a cell has never been assigned, return 0 instead of raising an error.
Part 2: Spreadsheet With Formula Cells
Constraints
- 0 <= len(operations) <= 2000
- 1 <= length of each formula string <= 200
- Cell IDs contain one or more uppercase letters followed by one or more digits
- All formulas are syntactically valid
- Formula tokens are integers, cell references, +, -, and optional spaces
Examples
Input: [['SET', 'A1', '10'], ['SET', 'B1', 'A1 + 5'], ['GET', 'B1'], ['SET', 'A1', '20'], ['GET', 'B1'], ['SET', 'C1', 'B1 + A1 - 3'], ['GET', 'C1']]
Expected Output: ['15', '25', '42']
Explanation: Dependent formulas should reflect updated source values.
Input: [['SET', 'A1', 'B1 - 2'], ['GET', 'A1'], ['GET', 'B1']]
Expected Output: ['-2', '0']
Explanation: Unset referenced cells evaluate to 0, so A1 becomes 0 - 2.
Input: [['SET', 'A1', '1'], ['SET', 'B1', 'A1 + 2'], ['SET', 'A1', 'B1 + 3'], ['GET', 'A1'], ['GET', 'B1']]
Expected Output: ['CYCLE', '1', '3']
Explanation: The third operation would create A1 -> B1 -> A1, so it is rejected and previous values remain.
Input: [['SET', 'A1', 'A1 + 1'], ['GET', 'A1']]
Expected Output: ['CYCLE', '0']
Explanation: A direct self-reference is also a cycle, and the failed update leaves A1 unset.
Input: []
Expected Output: []
Explanation: Edge case: no operations.
Hints
- Model dependencies as a directed graph from a formula cell to the cells it references.
- Use DFS both to evaluate formulas and to detect whether a new formula introduces a cycle.