Design Spreadsheet Formula Cells
HarveySoftware Engineer · OnsiteLast updated
This question evaluates ability to design and implement data structures and algorithms for expression parsing, dependency tracking, incremental evaluation, and cycle detection in an in-memory spreadsheet.
Design an in-memory spreadsheet that supports assigning either literal integer values or formulas to cells.
Cells are identified by names such as A1, B1, and C12. A formula is an expression made of cell references and integer constants joined by +, for example:
A1 = B1 + C1 + 5
Implement an API similar to:
setCell(cellName, valueOrFormula)
getCell(cellName) -> int
Requirements:
setCellmay assign either:
- an integer value, such as
10, or - a formula, such as
B1 + C1.
getCellreturns the current evaluated integer value of the requested cell.- If a referenced cell has not been assigned, treat its value as
0. - When a cell is updated, future reads should reflect the updated dependencies.
- Follow-up: detect circular dependencies between formulas and reject updates that would create a cycle. For example, the following should be rejected because it creates a cycle:
A1 = B1 + C1
C1 = A1
Explain your data structures and implement the API.
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.