OpenAI questions

Spreadsheet with Formulas

OpenAIPhone screenMedium

A Spreadsheet stores cells addressed by names like "A1" or "AA10": one or more uppercase letters followed by a row number. Each cell's content is either an integer literal such as "5" or "-3", or a formula starting with "=" that adds integer literals and cell references with "+", such as "=A1+B2+7". A cell that was never set has value 0.

Part 1: Cells and formulas

Implement a Spreadsheet class with two methods:

  • set_cell(cell, content) stores content for cell, replacing any previous content.
  • get_cell(cell) returns the cell's computed value.

An integer literal evaluates to itself. A formula evaluates to the sum of its terms, where each cell reference contributes that cell's current value. A formula's value always reflects the current values of the cells it references, directly or indirectly.

  1. Example 1
    init
    []
    operations
    [["set_cell",["A1","5"]],["set_cell",["B1","=A1+2"]],["get_cell",["B1"]],["set_cell",["A1","10"]],["get_cell",["B1"]]]
    Output
    [null,null,7,null,12]
  2. Example 2
    init
    []
    operations
    [["set_cell",["A1","=B2+7+C3"]],["get_cell",["A1"]],["set_cell",["C3","3"]],["get_cell",["A1"]]]
    Output
    [null,7,null,10]

Constraints

  • 1 <= len(operations) <= 1000
  • cell is one or more uppercase letters ('A'-'Z') followed by a row number starting at 1
  • 1 <= len(content) <= 100
  • A formula content has at most 10 terms separated by +
  • -10^9 <= term <= 10^9 for each integer term