FastPrepSpreadsheet Formula Dependencies

Spreadsheet Formula Dependencies

Citadel logoCitadel● HardFULLTIMEPHONE SCREEN

Updated

Problem statement

Start with an empty in-memory spreadsheet. Each cell holds either an integer or a formula referring to other cells. Process operations in order and return one string for every operation.

In this exercise, each formula has two operands and one operator. Each operand can be a cell ID or an integer. A formula can refer to another cell that also contains a formula.

  • ["SET", cell, value] replaces the cell with the integer value and returns "OK".
  • ["FORMULA", cell, left, operator, right] proposes replacing the cell with left operator right. The operator is +, -, *, or /. Accept the replacement and return "OK" unless it introduces a direct or indirect circular reference. In that case, return "CYCLE" and leave the spreadsheet completely unchanged.
  • ["GET", cell] returns the cell's current value as a decimal string. A cell that has never been assigned has value 0. Values must reflect every earlier accepted update, including updates to indirect dependencies.

Arithmetic is integer arithmetic. Division truncates toward zero. Return "ERROR" when evaluating a cell requires division by zero, either in that cell’s formula or in a cell it depends on. Such a formula may be stored successfully; the error is reported when its value is read. A later update can repair it.

Using the same cell more than once in a formula is legal and is not itself a cycle. Replacing a formula removes its old dependency relationships. Rejected updates must not change definitions or cached values.

In a read-heavy workload, how can per-query memoization be improved with a persistent cache and reverse dependencies? Explain which cached cells must be invalidated after an accepted update and how overwriting a formula changes the dependency graph.

Function

runSpreadsheet(operations: String[][]) → String[]

Examples

Example 1

operations = [["SET","A1","3"],["FORMULA","B1","A1","*","2"],["GET","B1"],["SET","A1","5"],["GET","B1"]]return = ["OK","OK","6","OK","10"]

The formula in B1 doubles the current value of A1. Its second read must use the updated value rather than a stale cached result.

Example 2

operations = [["FORMULA","A1","B1","+","B1"],["GET","A1"],["SET","B1","4"],["GET","A1"],["FORMULA","B1","A1","+","1"],["GET","B1"]]return = ["OK","0","OK","8","CYCLE","4"]

Repeating B1 in the first formula is legal. Replacing B1 with a reference to A1 would create a cycle, so that update is rejected and B1 remains 4.

Example 3

operations = [["SET","Z1","0"],["FORMULA","A1","7","/","Z1"],["GET","A1"],["SET","Z1","2"],["GET","A1"],["FORMULA","B1","-7","/","2"],["GET","B1"]]return = ["OK","OK","ERROR","OK","3","OK","-3"]

The stored formula initially divides by zero. Updating its denominator repairs it. Both positive and negative division truncate toward zero.

Constraints

  • 0 <= operations.length <= 256.
  • At most 64 different cell IDs appear across all operations. Each one matches [A-Z][A-Z0-9]{0,3}.
  • Every operation has exactly one of the forms described above. Each number is written as a plain decimal integer: an optional leading minus sign, and no leading zeros except for 0 itself. Its absolute value is at most 10^6.
  • At every step, each part of a calculation that does not depend on a division by zero has an integer value in [-10^9, 10^9].
  • An operand never holds parentheses or extra operators. To build a larger expression, refer to cells that hold formulas. All IDs and operators are case-sensitive.

More Citadel Phone Screen questions

Similar questions at other companies

Citadel interview experiences

All Citadel interview questionsSee Citadel hiring insights
Saved
class Solution {
    public String[] runSpreadsheet(String[][] operations) {
        // Write your code here.
    }
}
Checking account…