Design Spreadsheet
Problem
A spreadsheet has 26 columns ('A'…'Z') and rows rows; every cell starts at 0. Implement setCell("AX", value), resetCell("AX") (back to 0), and getValue("=X+Y") where each of X, Y is a cell reference or a non-negative integer. An unset cell reads as 0.
getValue("=5+7"), setCell("A1",10), getValue("=A1+6"), setCell("B2",15), getValue("=A1+B2"), resetCell("A1"), getValue("=A1+B2")12, 16, 25, 15class Spreadsheet:
def __init__(self, rows):
self.cells = {} # cell name -> value, default 0
def setCell(self, cell, value):
self.cells[cell] = value
def resetCell(self, cell):
self.cells[cell] = 0
def _operand(self, tok): # cell ref or literal int
if tok[0].isalpha():
return self.cells.get(tok, 0)
return int(tok)
def getValue(self, formula):
x, y = formula[1:].split("+") # drop '=', split on '+'
return self._operand(x) + self._operand(y)
class Spreadsheet {
constructor(rows) {
this.cells = new Map(); // cell name -> value, default 0
}
setCell(cell, value) {
this.cells.set(cell, value);
}
resetCell(cell) {
this.cells.set(cell, 0);
}
operand(tok) { // cell ref or literal int
if (tok[0] >= "A" && tok[0] <= "Z")
return this.cells.get(tok) ?? 0;
return parseInt(tok, 10);
}
getValue(formula) {
const [x, y] = formula.slice(1).split("+"); // drop '=', split on '+'
return this.operand(x) + this.operand(y);
}
}
class Spreadsheet {
Map<String, Integer> cells = new HashMap<>(); // default 0
public Spreadsheet(int rows) { }
public void setCell(String cell, int value) {
cells.put(cell, value);
}
public void resetCell(String cell) {
cells.put(cell, 0);
}
private int operand(String tok) { // cell ref or literal
if (Character.isLetter(tok.charAt(0)))
return cells.getOrDefault(tok, 0);
return Integer.parseInt(tok);
}
public int getValue(String formula) {
String[] p = formula.substring(1).split("\\+"); // drop '='
return operand(p[0]) + operand(p[1]);
}
}
class Spreadsheet {
unordered_map<string, int> cells; // default 0
int operand(const string& t) { // cell ref or literal
if (isalpha(t[0])) {
auto it = cells.find(t);
return it == cells.end() ? 0 : it->second;
}
return stoi(t);
}
public:
Spreadsheet(int rows) { }
void setCell(string cell, int value) { cells[cell] = value; }
void resetCell(string cell) { cells[cell] = 0; }
int getValue(string formula) {
int plus = formula.find('+'); // formula is "=X+Y"
string x = formula.substr(1, plus - 1);
string y = formula.substr(plus + 1);
return operand(x) + operand(y);
}
};
Explanation
Even though the problem talks about a 26×rows grid, almost every cell stays 0, so allocating a full matrix is wasteful. Instead we store only the cells that have actually been written, using a hash map from the cell name (like "A1") to its integer value. Any cell missing from the map is, by definition, 0.
setCell(cell, value) and resetCell(cell) are one-liners: put value (or 0) into the map under that key. We do not need rows at all because the formulas only ever reference valid cells per the constraints.
getValue("=X+Y") drops the leading = and splits on + to get two operands. For each operand we ask one question: does it start with a letter? If yes it is a cell reference, so we look it up in the map and default to 0 when absent. Otherwise it is a non-negative integer literal and we just parse it. The answer is the sum of the two resolved operands.
This "store only what is touched" idea is the classic sparse representation: O(1) work per operation and memory proportional to the number of distinct cells written, not to the whole grid.
Example: getValue("=5+7") is 5+7 = 12 with no map lookups. After setCell("A1",10) and setCell("B2",15), getValue("=A1+B2") is 10+15 = 25. Then resetCell("A1") overwrites A1 with 0, so the same formula now gives 0+15 = 15.