Morgan Stanley Interview Question
Developer Program EngineersCountry: India
Interview Type: Phone Interview
public class inAnotinB {
public static void main(String[] args) {
int[] A = {1, 3, 4, 6,8,10, 17, 34} ;
int[] B = {2, 8, 17, 33, 44, 66, 89, 100, 123};
int largest = A[A.length-1] > B[B.length-1]? A[A.length-1] : B[B.length-1];
int smallest = A[A.length-1] < B[B.length-1]? A[A.length-1] : B[B.length-1];
int i=0,j=0;
while(smallest != largest) {
if(A[i] == B[j]) {
smallest = A[i];
i++;
j++;
} else if(A[i] < B[j]) {
System.out.print(A[i]+" ");
smallest = A[i];
i++;
} else {
smallest = B[j];
System.out.print(B[j]+" ");
j++;
}
if(i>=A.length) {
for (; j < B.length; j++) {
System.out.print(B[j] + " ");
smallest = B[j];
}
}
if(j>=B.length) {
for (; i < A.length; i++) {
System.out.print(A[i] + " ");
smallest = A[i];
}
}
}
}
}
The question is not very clear. Assuming you have a specified number of exchanges and the data is stock_ticker and stock_price, then you could create multiple hash maps of <string, double>, one per exchange. i.e. If exch==LIFFE then add the stock data to liffe_exch_map. If you're supposed to be aggregating prices from different exchanges then you only actually need one hash map, as exchange is irrelevant. If you didn't know the number of exchanges in advance then you could create a hashmap where the key is exchange and the returned value is another hashmap of <string, double>. Look up the correct list of stocks and their values based on the exchange, then look up the value for that stock as before.
It's also worth pointing out that you don't have to use a string as the key, you could get a lot more flexibility if you defined a proper Stock object to use as the key, and then defined the Stock hash function as needed, and let your container handle how it stores the Stock objects. For example, if the hash includes the exchange and name, then stocks with the same name but different exchanges would be stored in separate buckets in the hashmap. You then lookup a Stock object in the hashmap using name and exchange, and if your hashing function is working properly it's constant time lookup. If you stop caring about exchange then you simply remove exchange from the hash function.
For this types of questions, Its a biggest mistake to jump to solution directly. Its very necessary to ask right set of questions before committing any solution. As an example, here lot of things are unknown 1) Data are coming from stock exchange but where they are going, is it some centralized place where all the exchanges are sending the data 2) What is data here, is it just stockname, price, time stamp 3) how frequenlty data is sent and is it in some batches like 100 stock data per second ? 4) what if two stock exchanges same same data, do we need to merge them 5) what kinds of operations we want to do, do we just need to consolidate the dataset 6) can stock exchanges are wired to the place where they are sending data, if not we might need to go for File system storage and the later transfer them. 7) is stock data are realtime stock value or historical data, 8) are all stock exchanges reside in same time zone, what if stock exchange A is NYSE and stock exchange B is bombay stock exchange
- billu April 02, 2013once u get clarity for all these open questions, you should jump on for solution.