Implement simplified Excel with SET/GET/SUM
Company: Fuse Energy
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates understanding of data structures and algorithms for modeling and implementing a simplified spreadsheet, covering dependency graphs, formula evaluation, incremental updates, and cycle detection.
Constraints
- Columns are single uppercase letters A..Z.
- Rows are integers 1..1000.
- SET values are integers (may be negative).
- A range "X1:Y2" includes all cells with column in [X,Y] and row in [1,2], inclusive.
- SET and SUM both overwrite any prior literal or formula in the target cell.
- An unset cell evaluates to 0.
- Formulas may form cycles; a re-entrant cell contributes 0 during evaluation (evaluation always terminates).
Examples
Input: ([('SET','A1',1),('SET','B2',2),('SUM','C1',['A1:B2','B2']),('SUM','C1',['B2']),('SET','C1',5),('GET','C1'),('SUM','D1',['C1','A1']),('SET','A1',10),('GET','C1'),('GET','D1')],)
Expected Output: [1, 2, 5, 2, 5, 5, 6, 10, 5, 15]
Explanation: SUM C1 over A1:B2 (1+0+0+2) + B2 (2) = 5, then C1 is re-SUMmed to just B2 (2), then SET to literal 5. GET C1 = 5. D1 = C1(5)+A1(1) = 6. After SET A1=10, C1 is still a literal 5, so GET C1 = 5 and D1 = 5+10 = 15.
Input: ([('SET','A1',3),('SUM','B1',['A1']),('SUM','C1',['B1']),('GET','C1'),('SET','A1',7),('GET','C1')],)
Expected Output: [3, 3, 3, 3, 7, 7]
Explanation: Transitive chain C1->B1->A1. Initially C1 evaluates to 3. After SET A1=7, GET C1 re-evaluates the chain to 7, showing formulas reflect later updates.
Input: ([('SET','A1',1),('SUM','B1',['A1']),('SUM','A1',['B1']),('GET','A1'),('GET','B1')],)
Expected Output: [1, 1, 0, 0, 0]
Explanation: SUM A1=[B1] overwrites A1's literal, creating a cycle A1<->B1. Evaluating A1 re-enters A1 (contributes 0): A1 = B1 = A1(0) = 0, B1 = A1 = 0. Evaluation terminates.
Input: ([('SET','A1',1),('SET','B1',2),('SET','A2',3),('SET','B2',4),('SUM','D1',['A1:B2']),('GET','D1')],)
Expected Output: [1, 2, 3, 4, 10, 10]
Explanation: Range A1:B2 expands to A1,A2,B1,B2 = 1+3+2+4 = 10. GET D1 confirms 10.
Input: ([('GET','Z1000')],)
Expected Output: [0]
Explanation: Edge case: GET on a never-set boundary cell returns 0.
Input: ([],)
Expected Output: []
Explanation: Edge case: an empty operation list yields an empty result list.
Hints
- Keep two maps: cellId -> integer literal, and cellId -> list of constituent cell ids (a formula). A cell lives in at most one of them; SET/SUM should clear it from the other.
- Expand each SUM range into its constituent cell ids once, at SUM time, so GET only walks dependency edges instead of re-parsing ranges.
- Evaluate a formula recursively, summing the evaluated value of each constituent. Carry a visited set down the recursion; if you re-enter a cell already in it, return 0 to break the cycle.