FunctionRacerProblemsRace

Spreadsheet Sums Without Cycles

HardClassesPython, JavaScript, TypeScript, Java, C++

Implement a class SumSheet for a spreadsheet in which a cell holds either a number or a formula that adds up other cells. Cells are named like "B2", and a cell that was never set holds the number 0. Write these methods:

The tests call the methods in sequence and compare every return value. The expected list holds null for the constructor.

Example 1

SumSheet()
set_number("A1", 5)
set_number("A2", 7)
set_sum("B1", ["A1","A2","A1"]) returns true
get_value("B1") returns 17
set_sum("C1", ["B1","A2"]) returns true
get_value("C1") returns 24
set_sum("A1", ["C1"]) returns false
count_dependents("A1") returns 2
set_number("A2", 1)
get_value("C1") returns 12

B1 counts A1 twice, so it is 5 + 7 + 5 = 17. Making A1 add up C1 is refused, because C1 already depends on A1 through B1. Changing A2 to 1 flows through B1 into C1.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Classes problems