FastPrepMost Fun on a Four-City Trip
Problems/Most Fun on a Four-City Trip

Most Fun on a Four-City Trip

Google logoGoogle● HardFULLTIMEONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: A 2022 Google onsite report describes a trip planning task. Every city has a fun score, the flight list says which pairs of cities are linked both ways, and the goal is the largest total fun over a route through four cities. The author used a search with backtracking and memoization. The interviewer pushed for a polynomial method in the n^2 to n^3 range.

The report gives no limits, does not say whether a city may be visited twice, and does not say what to return when no trip exists. Here, the four cities must all be different, consecutive cities need a direct flight, and you may start anywhere. You return -1 when no trip exists. Example 1 reuses the report's own fun values for california and LA. The rest of the examples and the limits are our choice.

Our version is about 90% the same as the reported question.

Each city has a fun score: cities[i] scores fun[i]. flights lists the direct flights: a flight [a, b] lets you fly from a to b and from b to a.

A trip visits exactly 4 different cities, one after another, and every move from one city to the next must use a direct flight. You may start in any city. The fun of a trip is the total fun of its 4 cities.

Return the largest fun of any trip, or -1 if no trip of 4 different cities exists.

Function

maxTripFun(cities: String[], fun: int[], flights: String[][]) → int

Examples

Example 1

cities = ["california","LA","seattle","denver","austin"]fun = [10000,1000,3000,500,2000]flights = [["california","LA"],["LA","seattle"],["seattle","denver"],["denver","austin"],["california","seattle"]]return = 15500

The best trip is austin, denver, seattle, california: 2000 + 500 + 3000 + 10000 = 15500. Each step uses a flight. The trip LA, california, seattle, denver is also valid but gives only 1000 + 10000 + 3000 + 500 = 14500. austin only has a flight to denver, so a trip with austin must go austin, denver, seattle and then california or LA, and california adds more fun.

Example 2

cities = ["hub","north","east","south"]fun = [5,40,30,20]flights = [["hub","north"],["hub","east"],["hub","south"]]return = -1

Every flight touches hub. Any trip with 4 different cities would have to pass through hub twice, so there is no valid trip and the answer is -1.

Example 3

cities = ["A","B","C","D","E"]fun = [100,1,90,80,70]flights = [["A","C"],["B","C"],["D","C"],["D","E"]]return = 340

C is the only city linked to A, B and D. The trip A, C, D, E gives 100 + 90 + 80 + 70 = 340. The trip B, C, D, E gives only 241, and A, C, B is a dead end after three cities.

Constraints

  • 1 <= cities.length <= 500
  • fun.length == cities.length
  • 0 <= fun[i] <= 10^6
  • Each cities[i] has 1 to 10 English letters, and all city names are different. Names are case-sensitive.
  • 0 <= flights.length <= 1500
  • flights[j].length == 2
  • Each flight joins two different cities from cities, and no pair of cities appears in more than one flight.

More Google Onsite Interview questions

  • Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Distinct Subset SumsONSITE INTERVIEW · Seen Nov 2026
  • Playlist With No Same Artist in a RowONSITE INTERVIEW · Seen Nov 2026
  • Delete Nodes and Return Forest RootsONSITE INTERVIEW · Seen Nov 2026
  • Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • First and Last Target Position in a Mountain ArrayONSITE INTERVIEW · Seen Nov 2026
  • Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • 0/1 Knapsack with Weight and Volume LimitsONSITE INTERVIEW · Seen Nov 2026
  • Batched Subsequence Word MatchesONSITE INTERVIEW · Seen Nov 2026

Similar questions at other companies

  • Mutual-Friend Referral RecommendationsFireworks AI · PHONE SCREEN · ONSITE INTERVIEW
  • Mutual-Friend RecommendationsMeta · PHONE SCREEN · ONSITE INTERVIEWMeta interview questions
  • Top-Down Employee Reporting ForestOkta · ONSITE INTERVIEWOkta interview questions
  • Accounts MergeOracle · ONSITE INTERVIEWOracle interview questions
  • Evolving Merchant Clusters and Persistent PinsStripe · OAStripe interview questions
  • Top Mutual-Friend RecommendationsVisa · OAVisa interview questions
  • Top Mutual-Friend RecommendationsSuperhuman · OASuperhuman interview questions
  • Assign Campers to the First Arriving CarAtlassian · PHONE SCREENAtlassian interview questions
  • Rooms With Two Other Entrances and a Treasure SuccessorWalmart · PHONE SCREENWalmart interview questions
  • Resource Transition ProbabilitiesWhatNot · PHONE SCREENWhatNot interview questions

Google interview experiences

  • Google interviews · November 2026
  • Google Software Engineer Onsite Questions: 10 Experiences
  • Google OA: What It Tests (2026)
  • Google L4 SWE Phone Screen: 1 Experience
  • Google ML Engineering Manager Onsite: 1 Experience
  • Google Experienced SWE First Round and Onsite: 1 Experience
All Google interview questionsSee Google hiring insights
Saved
class Solution {
    public int maxTripFun(String[] cities, int[] fun, String[][] flights) {
        // Write your code here.
    }
}
Checking account…