cswork / libraryOpen practice workspace

Interview questions · Amazon

Spreadsheet Formulas with Dependency Updates

Implement spreadsheet SET, SUM, and GET operations with live formulas, repeated-reference multiplicity, and efficient dependency updates.

Implement spreadsheet_get_results(row_count: int, column_count: int, operations: list[list[str]]) -> list[int].

The spreadsheet initially contains zeros. Process operations in order and return the value produced by every GET.

Operation Format

  • ["SET", cell, value] stores the signed decimal integer value in cell and removes any formula previously stored there.
  • ["SUM", cell, reference1, ...] stores a dynamic sum formula in cell. Each reference is one cell such as "B3" or an inclusive rectangle such as "A1:C2".
  • ["GET", cell] reads the cell's current value.

Input Domain

  • 1 <= row_count <= 1,000 and 1 <= column_count <= 26.
  • Cell names use columns A through the configured final column and one-based rows.
  • 0 <= len(operations) <= 20,000.
  • Every operation and reference is valid, and each SUM has at least one reference.
  • A SUM operation never creates a direct or indirect dependency cycle.

Output Rules

  • Formulas remain live: changing a referenced cell changes all dependent results.
  • Each appearance of a cell contributes once, so overlapping or repeated references contribute repeatedly.
  • SUM and SET replace the target's prior literal or formula.
  • Preserve GET order; return an empty list when there are no GET operations.
  • All intermediate and returned values fit signed 64-bit integers.

Constraints

  • Do not recompute every spreadsheet cell after each operation.
  • Exact values and dependency semantics are required.

Examples

Example 1

Input: row_count = 3, column_count = 3, operations = [["SET","A1","2"],["SUM","C3","A1","A1:B2"],["GET","C3"],["SET","B2","3"],["GET","C3"]]

Output: [4,7]

Example 2

Input: row_count = 2, column_count = 2, operations = [["SUM","B2","A1:A2"],["SET","A1","5"],["GET","B2"],["SET","B2","4"],["SET","A2","7"],["GET","B2"]]

Output: [5,4]

Hint: Store dependency multiplicity

Expand formula references into source-cell counts, and either propagate value deltas through reverse dependencies or evaluate the acyclic graph with invalidation.

Community answers

Nothing here yet.

Join in on CSWork →