Most Fun on a Four-City Trip
Updated
Problem statement
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[][]) → intExamples
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 = 15500The 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 = -1Every 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 = 340C 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 <= 500fun.length == cities.length0 <= 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 <= 1500flights[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