---------- Forwarded message ----------
From: Hai Yi
Date: Wed, Oct 8, 2014 at 9:42 AM
Subject: onsite BofA interview, continuance of the phone interview on 10/01/14
From: Hai Yi
Date: Wed, Oct 8, 2014 at 9:42 AM
Subject: onsite BofA interview, continuance of the phone interview on 10/01/14
Coding on the computer and white board
1) code on Eclipse
There is a folder containing stock files, each line of which includes info as below, and delimiter is "\t".
date in the format "YYYY-MM-DD"
indicator (BUY/SELL), this is counter party BUY from or SELL to BofA
symbol
counter party
quantity
price
There are 100 such files and each file contains 10,000 lines.
In the same folder there is also a short file, marks.txt in which there are two columns in each line showing the symbol and market price, for example:
AAPL 350.00
Task: reading the file and figure out the long positions that BofA hold on the stocks, list top 20 in the descendant order with regard to value (quantity*price) and the respective market value. For instance, BofA might buy/sell AAPL, if the net is long, you have to figure it out and count it as required above.
2) white board
Create a high efficient cache solution for computation, it must be thread safe.
public interface Computable <K, V> {
public V compute(K k);
}
public class Cache <K,V> implements Computable<K,V>{
//the Caculator below is a delegate to do the heavy lift, but the result can be cached so it's supposed to call only once
private Computable<K,V> caculator = new Caculator<K,V>();
private Map<K,V> cache = new HashMap<K,V>();
public V compute(K k){
if(cache.contains(k){
return cache.get(k);
}
V val = caculator.compute(k);
cache.put(k,val);
return val;
};
}
What's the problem with the above implementation? How to make it highly performant and thread safe?
I wrote a good implementation with synchronization and he complimented "nearly perfect" but he continue to request to create a "lock-free" solution, I couldn't do it and he wrote one using FutureTask, I don't remember the details. And he also suggested a even better one using AKKA's actor.
I didn't say but IMHO the common idea that synchronization is a performance penalty is just a urban legend - with the nowaday's modern Java compiler the overhead of synchronization is just trivial.
3) White board Singlton pattern.
4) Eclipse a Lock implementation, use only AtomicBoolean
public interface Lock {
public void lock();
public void unlock();
}
public class LockImpl implements Lock{
private AtomicBoolean bool = new AtomicBoolean();
public void lock(){
//What's in here?
}
public void unlock(){
//what's in here?
}
}