Design Excel Sum Formula
Now let's add formulas! Make a cell show the sum of other cells. sum(row, col, ["A1", "B1"]) sets a formula that adds those cells. get() on that cell returns the computed sum. If a referenced cell changes, the sum auto-updates. Example: set(1, "A", 5); set(1, "B", 3) sum(1, "C", ["A1", "B1"]) -> C1 = A1 + B1 get(1, "C") -> returns 8
Under the hood, Design Excel Sum Formula is about traversing relationships without getting trapped in cycles. It's a hard one for a reason: it stresses modeling relationships when the rules pile up. If you can explain your visited policy, you're already ahead of most candidates.
Examples
You write
Excel: set(row: int, column: str, val: int), get(row: int, column: str), sum(row: int, column: str, numbers: List[str])
Example 1
- Input
- Excel(3,"C"), set(1,"A",5), get(1,"A")
- returns
- 5
Example 2
- Input
- Excel(3,"C"), set(1,"A",2), set(1,"B",3), get(1,"B")
- returns
- 3
The pattern behind it
Design
I reach for it when
- The statement describes a class with methods, not one function.
- Each method needs to be fast, and the structure decides how fast.
Say this before you type. Say each method, the cost you want from it, and the structure that earns that cost.
All 27 Design problemsGraphs
I reach for it when
- The input is edges, prerequisites or connections.
- You need cycles, an ordering or connected groups.
Say this before you type. Say whether the edges are directed, and which of the three jobs you are doing.
All 32 Graphs problems
Meet Coco
Coco is an AI study partner, not a person. She listens while you explain your code, asks what a line does and never hands you the fix. Sign in free to type with Coco, up to a daily limit. The editor and Run stay open without an account. Anyone can open the editor and run their code.
Each locked part opens in the editor once the part before it passes.