Implement a Formula Spreadsheet
Company: Harvey
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
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.
Part 1: Plain Integer Spreadsheet
Constraints
- 0 <= len(operations) <= 100000
- All labels in the input are valid spreadsheet labels: uppercase letters followed by a positive row number
- Stored values are integers in the 32-bit signed range
- Only SET and GET operations appear
Examples
Input: []
Expected Output: []
Explanation: There are no operations, so there are no GET results to return.
Input: [('SET', 'A1', 10), ('GET', 'A1')]
Expected Output: [10]
Explanation: Cell A1 is set to 10, so the GET returns 10.
Hints
- A hash map from label to integer is enough for this part.
- You only need to add something to the output when you see a GET operation.
Part 2: Spreadsheet with Addition Formulas
Constraints
- 0 <= len(operations) <= 10000
- All labels in the input are valid spreadsheet labels
- Formula strings contain no spaces and use only integer literals, valid cell references, and '+'
- For this part, dependency graphs are acyclic
- A later SET can overwrite any earlier cell content
Examples
Input: [('SET', 'A1', 10), ('GET', 'A1')]
Expected Output: [10]
Explanation: A1 is set to 10, so GET A1 returns 10.
Input: [('SET', 'A1', 10), ('SET', 'A1', 30), ('GET', 'A1')]
Expected Output: [30]
Explanation: The second SET overwrites the first value.
Hints
- Store the raw value for each cell. If it is a formula, evaluate it only when needed.
- Split a formula on '+' and process each token as either an integer literal or another cell reference.
Part 3: Spreadsheet with Cycle Detection
Constraints
- 0 <= len(operations) <= 10000
- All labels in the input are valid spreadsheet labels
- Formula strings contain no spaces and use only integer literals, valid cell references, and '+'
- Cycles may exist and must be reported on GET
- A later SET can overwrite any earlier cell content
Examples
Input: [('SET', 'A1', 'B1+1'), ('SET', 'B1', 'A1+1'), ('GET', 'A1')]
Expected Output: ['CYCLE']
Explanation: A1 depends on B1, and B1 depends back on A1.
Input: [('SET', 'A1', 'A1+1'), ('GET', 'A1')]
Expected Output: ['CYCLE']
Explanation: A direct self-reference is also a cycle.
Hints
- During DFS evaluation, keep a set of cells currently on the recursion stack.
- Use two states: one structure for cells already fully computed in this GET, and one for cells currently being visited.