Spreadsheet Formula Evaluator
Updated
Problem statement
You are building a small spreadsheet. Its cells are named A1 through Z26. You are given an array of commands, commands. Each entry is one command, and you run the commands in the order they appear. Return an array with one result string for each command, in the same order.
The commands
SET cell expression: replace what the cell holds. The expression may start with an optional=. After that come one or more terms joined by+. A term is a nonnegative whole number or a cell label. Spaces around the equals sign and around the terms are optional.GET cell: work out the value of the cell from the current state of the spreadsheet. If its formula uses other cells, work out their values the same way.
The results
- A
SETthat works gives"OK". - If a
SETwould make a cell depend on itself, directly or through other cells, it gives"ERROR". Nothing in the spreadsheet changes. - A
GETgives the value as a decimal string. It gives"ERROR"if the cell was never set, or if working out the value reaches a cell that was never set. - A formula can be saved before the cells it uses are set. A later
SETcan make it work.
For example, take SET A1 1, SET B1 = A1 + 2, SET C1 = B1 + 3, SET A1 = C1 + 4, GET A1, GET C1. The first three commands give "OK", and the cells hold 1, 3 and 6. The fourth command would make A1 depend on C1, which depends on B1, which depends on A1. That is a loop, so it gives "ERROR" and A1 still holds 1. The two GET commands give "1" and "6". The answer is ["OK", "OK", "OK", "ERROR", "1", "6"].
Function
runSpreadsheet(commands: String[]) → String[]Examples
Example 1
commands = ["SET A1 10", "SET A1 20", "SET B1 = A1 + 10", "GET A1", "GET B1", "SET A1 = A1 + 10", "GET A1"]return = ["OK", "OK", "OK", "20", "30", "ERROR", "20"]There are seven commands, so the answer has seven results. The second command replaces A1 with 20. B1 is A1 + 10, so GET B1 gives 30, and GET A1 gives 20. SET A1 = A1 + 10 makes A1 depend on itself, so it gives ERROR and nothing changes. The last GET A1 still gives 20. The answer is ["OK", "OK", "OK", "20", "30", "ERROR", "20"].
Example 2
commands = ["SET A1 2", "SET B1 = A1 + A1 + 1", "SET C1 = B1 + A1", "GET C1", "SET A1 5", "GET C1"]return = ["OK", "OK", "OK", "7", "OK", "16"]At first A1 is 2, B1 is 2 + 2 + 1 = 5, and C1 is 5 + 2 = 7. After SET A1 5, the values are worked out again from the new state. B1 is 5 + 5 + 1 = 11 and C1 is 11 + 5 = 16. The answer is ["OK", "OK", "OK", "7", "OK", "16"].
Example 3
commands = ["SET A1 1", "SET B1 = A1 + 2", "SET C1 = B1 + 3", "SET A1 = C1 + 4", "GET A1", "GET C1"]return = ["OK", "OK", "OK", "ERROR", "1", "6"]A1 is 1, B1 is 3 and C1 is 6. Setting A1 to C1 + 4 would make a loop: A1 uses C1, C1 uses B1, and B1 uses A1. That SET gives ERROR and changes nothing, so GET A1 gives 1 and GET C1 gives 6. The answer is ["OK", "OK", "OK", "ERROR", "1", "6"].
Constraints
- The spreadsheet has exactly 26 columns
AthroughZand 26 rows1through26. 1 <= commands.length <= 10000- The total number of expression terms is at most
200000. - Every command is written correctly, every cell label is inside the spreadsheet, and every value that is worked out successfully fits in a signed 32-bit integer.
- Expressions contain only nonnegative whole numbers, cell labels, optional spaces, and the
+sign.
Similar questions at other companies
- Spreadsheet With Dependent CellsCharacter.AI · PHONE SCREEN
- Spreadsheet Formula DependenciesAmazon Web Services · ONSITE INTERVIEWAmazon Web Services interview questions
- Evaluate Live Spreadsheet ExpressionsStrala · PHONE SCREEN
- Mutual Station Connectivity QueriesBloomberg LP · PHONE SCREENBloomberg LP interview questions
- Resolve Variable Equations with Dependency ErrorsApplied Intuition · PHONE SCREENApplied Intuition interview questions
- Resolve Variable Equations with Dependency ErrorsMotive · PHONE SCREENMotive interview questions
- Evolving Merchant Clusters and Persistent PinsStripe · OAStripe interview questions
- N-Ary Tree Codec OperationsLinkedIn · ONSITE INTERVIEWLinkedIn interview questions
- Traveling the GraphsOptiver · OAOptiver interview questions
- Periodic-Color Binary Forest RootsGoogle · PHONE SCREENGoogle interview questions