Goldman Sachs Interview Question
Software Engineer / DevelopersWe will have to use Observer pattern. Each cell is an Observable and will have a list of Observers.
Whenever the Observable object value is changed, the Observers will be notified and their value get changed and so on.
"The observer pattern is a software design pattern in which an object, called the subject, maintains a list of its dependents, called observers, and notifies them automatically of any state changes, usually by calling one of their methods. It is mainly used to implement distributed event handling systems. "
Hi
I think below will also work
public class ExcelCellApp {
//inner Class Cell to model Excel Cells
public class Cell
{
int data;
public int getData()
{
return data;
}
}
public static void main(String[] args) {
ExcelCellApp app = new ExcelCellApp();
Cell A1=app.new Cell();
Cell B1=app.new Cell();
Cell C1=app.new Cell();
Cell D1=app.new Cell();
//Here need to Set A1 cells data
A1.data=2;
System.out.println(" A1 cell data "+A1.getData());
B1.data=A1.getData()+3;
C1.data=A1.getData()*2;
System.out.println(" B1 cell data "+B1.getData()+" C1 cell data "+C1.getData());
D1.data=C1.getData()+B1.getData();
System.out.println(" D1 cells Data "+D1.getData());
}
}
Maintain a dependency graph. Each formula gives you the dependent and dependee.
- Anonymous August 18, 2011Once you update the value of a cell, do a topological sort starting from that node, and update the values in that order.