Phone interview 1:
class UndergroundSystem {
private final Map<Integer, PassengerEntranceInfo> passengerCommuteInfo;
private final Map<String, StationsAvgTimeInfo> averageTravelTime;
public UndergroundSystem() {
passengerCommuteInfo = new HashMap();
averageTravelTime = new HashMap();
}
public void checkIn(int id, String stationName, int t) {
passengerCommuteInfo.put(id, new PassengerEntranceInfo(stationName, t));
}
public void checkOut(int id, String stationName, int t) {
PassengerEntranceInfo passengerEntranceInfo = passengerCommuteInfo.get(id);
int startingTime = passengerEntranceInfo.getTime();
String startingStation = passengerEntranceInfo.getStation();
if(averageTravelTime.containsKey(startingStation+"-"+stationName)){
StationsAvgTimeInfo stationsAvgTimeInfo = averageTravelTime.get(startingStation+"-"+stationName);
int totalPassengers = stationsAvgTimeInfo.getTotalPassengers();
double totalTime = stationsAvgTimeInfo.getAvgTime() * totalPassengers;
totalTime += t-startingTime;
averageTravelTime.put(startingStation+"-"+stationName, new StationsAvgTimeInfo(totalPassengers+1,totalTime/(totalPassengers+1)));
} else {
averageTravelTime.put(startingStation+"-"+stationName, new StationsAvgTimeInfo(1, t-startingTime));
}
}
public double getAverageTime(String startStation, String endStation) {
return averageTravelTime.get(startStation+"-"+endStation).getAvgTime();
}
private static class PassengerEntranceInfo {
private final String station;
private final int time;
PassengerEntranceInfo(final String station, final int time) {
this.station = station;
this.time = time;
}
String getStation() {
return station;
}
int getTime() {
return time;
}
}
private static class StationsAvgTimeInfo {
private final int totalPassengers;
private final double averageTime;
StationsAvgTimeInfo(final int totalPassengers, final double averageTime) {
this.totalPassengers = totalPassengers;
this.averageTime = averageTime;
}
int getTotalPassengers() {
return totalPassengers;
}
double getAvgTime() {
return averageTime;
}
}
}Phone interview 2:
Onsite
two interviews
First question was okay, but not great. I don't think I will get offer.
Only two rounds.