Amazon Interview Question
SDE1sCountry: United States
Interview Type: Phone Interview
Not a homework. This is what I wrote and couldn't wrap my head around which data structure to use as my approach would be quite inefficient.
class Car
{
public string CarName { get; set; }
public double MilesDriven { get; set; }
public int GallonFilled { get; set; }
public DateTime DateFilled { get; set; }
}
class MPGCalc
{
Dictionary<string, List<Car>> log = new Dictionary<string, List<Car>>();
public void LoadData(string inp)
{
string[] lines = inp.Split(new[] { Environment.NewLine },StringSplitOptions.None);
string personName, carName;
double milesDriven;
int gallonsFilled;
DateTime fillupDate;
List<Car> carList = new List<Car>();
foreach(var line in lines)
{
string[] info = line.Split(',');
//(#person,carName,milesDriven,gallonsFilled,fillupDate)
personName = info[0];
carName = info[1];
Double.TryParse(info[2], out milesDriven);
int.TryParse(info[2], out gallonsFilled);
DateTime.TryParse(info[4], out fillupDate);
if(log.ContainsKey(info[0]))
{
log.TryGetValue(personName, out carList);
carList.Add(new Car {CarName = personName, MilesDriven = milesDriven, GallonFilled = gallonsFilled, DateFilled = fillupDate});
log[personName] = carList;
}
else
{
carList.Add(new Car {CarName = personName, MilesDriven = milesDriven, GallonFilled = gallonsFilled, DateFilled = fillupDate});
log.Add(personName, carList);
}
}
}
}
This sounds awfully lot like a homework problem.
- prudent_programmer May 16, 2018