KatInterview

Spreadsheet Cells With Sum Formulas and Loop Detection

Data Structure DesignAuto-graded · 13 test cases
MediumAuto-gradedData Structure Designgraphdfscycle-detection

Last updated

Simulate a command list on a spreadsheet whose cells hold integers or two-reference sum formulas, evaluating lazily and reporting circular references.

Spreadsheet Command Simulator

You are given a log of commands to run against an initially empty spreadsheet. Each cell stores a text value that is either a plain integer or a sum formula. Return what each command produces.

Commands

The two parallel lists operations and arguments describe the log. The command at index i has the name operations[i] and the argument list arguments[i].

  • "set_cell" with arguments [cell_id, value]: store the text value in the cell cell_id, replacing any earlier content. Nothing is evaluated at this point. The result for this command is None.
  • "get_value" with arguments [cell_id]: compute the integer value of that cell. The result is that integer, or the string "ValueError" if the evaluation fails (see below).

The output list has one entry per command, in order.

How a cell is evaluated

  • Never set: a cell that has not been set evaluates to 0.
  • Plain integer: if the stored text does not start with =, convert it with Python's int(...). If the conversion fails, the evaluation fails.
  • Formula: if the stored text starts with =, it must match exactly =X+Y, where X and Y are each one uppercase letter followed by one or more digits (for example =A1+B10). The value is the value of X plus the value of Y, each evaluated by these same rules. Any other formula text is invalid, and the evaluation fails. For example, =B1 has only one reference and is invalid, and so is a formula with spaces.
  • Circular reference: if, while evaluating a cell, you come back to a cell that is still being evaluated higher up the chain, the evaluation fails. This includes a cell that refers to itself.
  • Shared dependencies are not cycles: a cell may be reached by several different paths, as in D = B + C where both B and C use A. This is fine, because only a cell that is still in progress counts as a loop.

If any cell reached during a get_value fails, the whole get_value returns "ValueError". A failure in one query does not affect later commands. Cells may be overwritten freely, and a later get_value uses the current contents.

Task

Implement the function that runs all commands and returns the list of results. Each entry is None for set_cell, an int for a successful get_value, or the string "ValueError" for a failed get_value.

Constraints

  • Cell IDs are an uppercase letter followed by a number, from A1 to Z99.
  • operations and arguments have the same length, and every operation is "set_cell" or "get_value".
  • Referenced cells that were never set count as 0.
  • A cell's dependency chain can be up to a few thousand cells long, so avoid solutions that overflow the recursion limit or take exponential time on shared dependencies.

Examples

Example 1

Input: run_sheet_commands(['set_cell', 'set_cell', 'set_cell', 'get_value', 'set_cell', 'set_cell', 'get_value'], [['A1', '10'], ['B1', '20'], ['C1', '=A1+B1'], ['C1'], ['A1', '=B1'], ['B1', '=A1'], ['A1']])

Output: [None, None, None, 30, None, None, 'ValueError']

Hints

Hint 1

Store the raw text for each cell in a dictionary and evaluate only when a get_value command arrives.

Hint 2

Use a depth-first evaluation with a set of cells currently being evaluated. Seeing a cell already in that set means a circular reference. Remove a cell from the set once it finishes, so shared dependencies are not mistaken for cycles.

Hint 3

Match each formula against the exact pattern for two references, and treat anything else as an error. Within one query, cache finished cells so repeated shared dependencies are not re-evaluated, and raise the recursion limit for long chains.