Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7564031
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T13:48:49+00:00 2026-05-30T13:48:49+00:00

Just had a phone interview with TripAdvisor (and didn’t make the cut). I was

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T13:48:51+00:00Added an answer on May 30, 2026 at 1:48 pm

    1) Your searchList() method doesn’t work (or even compile). There is no locationId field on List, 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 Map of 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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just had a quick phone interview. The interviewer asked me a few questions
I had written a small utility app just for my phone, which stopped the
Given a phone keypad as shown below: 1 2 3 4 5 6 7
Just had a discussion at work about the merits of using PostgreSQL over MySQL
just had a general question about how to approach a certain problem I'm facing.
I just had an idea, is that possible to protect my java sources (packed
I just had an interesting experience with a startup form in MS Access 2010.
I just had the jQuery epiphany the other day and still feel like there
I just had a quick question about git - how backwards compatible are git
I just had a ridonkulous typo in my iPhone app, answered here . Now

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.