Goldman Sachs Interview Question
Software Engineer / DevelopersCountry: India
Interview Type: Phone Interview
We can implement this scenario with an observer pattern. The observed object taking in the number of stocks to be bought. The same object will also maintain a list of observers (the exchanges so to say). As soon as the observed object receives a buy order of say n stocks, it will notify all observers (stock exchanges), which in return will return the best selling price from them. This price can be compared at the observed object's end and the first order can be executed at the exchange offering the best selling (lowest) price. Do this recursively for n number of orders and your entire buy order will be executed at the lowest price. You can perform the same scenario for a sell order of n stocks as well.
We can implement this scenario with an observer pattern. The observed object taking in the number of stocks to be bought. The same object will also maintain a list of observers (the exchanges so to say). As soon as the observed object receives a buy order of say n stocks, it will notify all observers (stock exchanges), which in return will return the best selling price from them. This price can be compared at the observed object's end and the first order can be executed at the exchange offering the best selling (lowest) price. Do this recursively for n number of orders and your entire buy order will be executed at the lowest price. You can perform the same scenario for a sell order of n stocks as well.
Suppose we could take a snapshot of the current prices across the exchanges and capture them in the following format:
public class MarketSnapshot {
public PriceStack[] PriceStacks;
}
public class PriceStack {
public int[] MaxQtys;
public double[] Prices;
}
Then, we construct an increasing sequence of prices points as follows:
public class PricePoint {
public int Exchange;
public int MaxQty;
public double Price;
}
private static List<PricePoint> getSortedPricePoints(MarketSnapshot snapshot) {
List<PricePoint> pricePoints = new ArrayList<PricePoint>();
for(int i=0; i < snapshot.PriceStacks.length; i++) {
PriceStack priceStack = snapshot.PricePoints[i];
for (int j=0; j < priceStack.Prices.length; j++) {
pricePoints.add(new PricePoint(i, priceStack.MaxQtys[j], priceStack.Prices[j]);
}
}
pricePoints.sort((p1, p2) -> p1.Price.compareTo(p2.Price));
return pricePoints;
}
Then we simply buy starting from the left hand side of this list, until we have bought enough quantity:
private static void buyAtMarket(int qty, MarketSnapshot currentSnapshot) {
List<PricePoint> pricePoints = getSortedPricePoints(currentSnapshot);
int bought = 0;
int i = 0;
while(bought < qty && i < pricePoints.size()) {
bought += pricePoints.get(i++);
}
}
We can implement this scenario with an observer pattern. The observed object taking in the number of stocks to be bought. The same object will also maintain a list of observers (the exchanges so to say). As soon as the observed object receives a buy order of say n stocks, it will notify all observers (stock exchanges), which in return will return the best selling price from them. This price can be compared at the observed object's end and the first order can be executed at the exchange offering the best selling (lowest) price. Do this recursively for n number of orders and your entire buy order will be executed at the lowest price. You can perform the same scenario for a sell order of n stocks as well.
- Amit Gupta October 20, 2016