Spreadsheet Formula Dependencies
Updated
Problem statement
Simulate a spreadsheet whose cells are labeled from A1 through Z26. Every cell starts unset, and an unset cell evaluates to 0.
You are given the commands in operations. Run them in order:
SET cell valuestores a signed integer in the cell. It replaces any formula the cell held before.FORMULA cell term+term+...stores a formula that adds up its terms. It replaces whatever the cell held before. Each term is either a nonnegative integer or a cell label. The same cell label may appear more than once.GET cellevaluates the cell using the current contents of the spreadsheet.
Formulas stay linked to the cells they use. When a cell changes, every later GET that depends on it, directly or through other formulas, sees the change. No formula ever makes a cell depend on itself, directly or through other cells. In other words, the dependency graph is acyclic.
Return one integer for every GET command, in command order.
Function
evaluateSpreadsheet(operations: String[]) → int[]Examples
Example 1
operations = ["SET A1 5","SET B1 7","FORMULA C1 A1+B1","GET C1","SET A1 10","GET C1"]return = [12,17]The first GET C1 adds 5 + 7 = 12. Then A1 changes to 10. C1 still holds its formula, so the second GET C1 gives 10 + 7 = 17.
Example 2
operations = ["SET A1 3","FORMULA B1 A1+A1+2","GET B1","FORMULA C1 B1+A1","GET C1","SET A1 4","GET C1"]return = [8,11,14]Repeated references count separately, so B1 = 3 + 3 + 2 = 8. Then C1 = 8 + 3 = 11. Updating A1 changes both levels, giving B1 = 10 and C1 = 14.
Example 3
operations = ["GET Z26","FORMULA A1 Z26+5","GET A1","SET Z26 2","GET A1","SET A1 -3","GET A1"]return = [0,5,7,-3]Z26 starts unset, so the first GET returns 0. The formula in A1 uses Z26, so the next two GET commands on A1 follow the value of Z26. One runs while Z26 is still unset, and one runs after it is set. The final SET replaces the formula in A1 with the plain number -3, which the last GET returns.
Constraints
1 ≤ operations.length ≤ 10000.- Every command uses a valid cell label from
A1throughZ26. - Each formula contains at least one term and at most
676terms. - The total number of formula terms across all commands is at most
200000. - Every formula assignment leaves the dependency graph acyclic.
- Every stored integer and every evaluated result fits in a signed 32-bit integer.
More Amazon Web Services Onsite Interview questions
Similar questions at other companies
- Spreadsheet With Dependent CellsCharacter.AI · PHONE SCREEN
- Evaluate Live Spreadsheet ExpressionsStrala · PHONE SCREEN
- Resolve Variable Equations with Dependency ErrorsApplied Intuition · PHONE SCREENApplied Intuition interview questions
- Resolve Variable Equations with Dependency ErrorsMotive · PHONE SCREENMotive interview questions
- JSON Schema Object ValidatorDocuSign · ONSITE INTERVIEWDocuSign interview questions
- N-Ary Tree Codec OperationsLinkedIn · ONSITE INTERVIEWLinkedIn interview questions
- Nested Template Variable ExpansionFigma · ONSITE INTERVIEWFigma interview questions
- Evolving Merchant Clusters and Persistent PinsStripe · OAStripe interview questions
- Spreadsheet Formula EvaluatorPika · PHONE SCREEN
- Nested Substitutions with Cycle DetectionKikoff · PHONE SCREENKikoff interview questions