Implement Spreadsheet with Cells
You are asked to design a simple Spreadsheet system consisting of multiple cells, where each cell can either contain a numeric value or depend on other cells by referencing them as children.
Each cell can be represented as an object:
class Cell {
Integer value;
String child1;
String child2;
}
If a cell contains a value, it’s a constant.
If it contains one or two child references, its value equals the sum of its children.
You are required to implement a class Spreadsheet that supports:
- Setting a cell with either a value or children references.
- Getting the calculated value of a given cell key.
Example
Input:
setCell("A", new Cell(null, "B", "C"));
setCell("B", new Cell(3, null, null));
setCell("C", new Cell(5, null, null));
getCellValue("A") → 8
If cells have nested references:
setCell("A", new Cell(null, "B", "C"));
setCell("B", new Cell(null, "D", null));
setCell("C", new Cell(2, null, null));
setCell("D", new Cell(4, null, null));
getCellValue("A") → 6
Key Idea
- Use DFS to traverse cell dependencies recursively.
- Use memoization to cache computed results.
- For dynamic updates, maintain a reverse dependency map (parent map) to efficiently recompute affected cells.
The VOprep team has long accompanied candidates through various major company OAs and VOs, including OpenAI, Google, Amazon, Citadel, SIG, providing real-time voice assistance, remote practice, and interview pacing reminders to help you stay smooth during critical moments. If you are preparing for Stripe or similar engineering-focused companies, you can check out our customized support plans—from coding interviews to system design, we offer full guidance to help you succeed.