Just had a phone interview with TripAdvisor (and didn’t make the cut).
I was given the code below and asked to implement findBestTravelAlert (in Java).
Given a list of TravelAlert objects, find the best travel alert to display for a specific location. See example at http://www.tripadvisor.com/Tourism-g147306-Haiti-Vacations.html
class TravelAlert {
int id;
String message;
int locationId;
}
class Location {
int locationId;
int parentLocationId; // -1 if no parent, location is hierarchy
String name;
static Location findLocation(int locationId) {...}
}
class TravelAlertStore {
private List<TravelAlert> lAlerts; // already populated, static
// Return null if no alert
TravelAlert findBestTravelAlert(int locationId) {
// implement this
}
}
Examples: If we have 3 travel alerts, one in Boston, one in Massachusetts, and one in the USA:
- A user looking at a Boston specific page should see the travel alert of Boston and not the one of Massachusetts
- A user looking at a Massachusetts page should see the travel alert of
Massachusetts - A user looking at a Newton specific page should see the
travel alert of Massachusetts - A user looking at a NYC specific page
should see the US travel alert - A user looking at a Montreal specific
page should see no travel alert.
The way I did it was rather naive, but it was all I could come up with under pressure:
Search through the list of TravelAlerts and see if the locationId matches that of our locationId. If not, repeat the search and check for matches with the locationId of the parent. If at any point in this process we find a match, return that TravelAlert. If we reach the end of the process (i.e. there is no parent location), return null.
Here’s the code:
TravelAlert findBestTravelAlert(int locationId) {
TravelAlert current_alert = searchList(locationId, lAlerts);
if(current_alert != null) {
return current_alert;
}
else {
int parentLocationId = findLocation(locationId).parentLocationId;
if(parent_locId == -1)
return null;
else {
return findBestTravelAlert(parentLocationId);
}
}
}
TravelAlert searchList(int locationId, List<TravelAlert> cur_list) {
if(cur_list == null) {
return null;
}
else if(cur_list.locationId == locationId) {
return cur_list;
}
else {
searchList(locationId, cur_list.next())
}
}
The time complexity is O(nk), where n is the length of the List of TravelAlerts and k is the height of the location hierarchy tree.
So, my question is:
- Is there a better algorithm for doing this, and
- Is there a better way of implementing my algorithm?
I’m sure the answer to the second question is yes; there must be built-in methods I could have used, were I more familiar with Java.
Any help would be appreciated. Good luck to all future applicants!
1) Your
searchList()method doesn’t work (or even compile). There is nolocationIdfield onList, and when you attempt to make the recursive call you are trying to pass an element from the list into the second parameter, where the list is expected.2) You haven’t followed either the Sun de-facto standard coding conventions, or the coding conventions of the example.
3) Generally, recursion is less efficient than looping (because you have to keep lots of stack frames around instead of, say, a single pointer/index into an array) – and since both of your recursive functions are tail-recursive they could both be replaced by loops.
Without building a better data structure (e.g. a
Mapof location id to travel alert), or knowing that the list of travel alerts is sorted (which you would probably have gained some credit for mentioning), you could probably save some time by pre-computing the list of location ids (that is, the chain from the location to the top of the tree) and then walking the travel alert list once (assuming it’s considerably bigger than the depth of the location tree) and comparing each alert with the list of location ids to determine the best match.