Fastest Favorite City Through a Required Vertex
Updated
Problem statement
An undirected weighted graph has roads [u,v,time] with positive travel times. Return the favorite city reachable in minimum total time from source.
If required is nonnegative, the route must visit that vertex; otherwise there is no required vertex. Break equal-time ties by smaller city ID. Return -1 if no favorite is reachable.
Function
fastestFavoriteCity(n: int, roads: int[][], favorites: int[], source: int, required: int) → intExamples
Example 1
n = 5roads = [[0,1,2],[1,2,2],[0,3,10],[2,4,1]]favorites = [3,4]source = 0required = -1return = 4Favorite 4 is reached in five units, faster than city 3.
Example 2
n = 5roads = [[0,1,1],[1,2,1],[0,3,1],[3,4,1],[2,4,10]]favorites = [2,4]source = 0required = 3return = 4Requiring city 3 makes favorite 4 the best.
Example 3
n = 3roads = [[0,1,2]]favorites = [2]source = 0required = -1return = -1The only favorite is unreachable.
Constraints
1 <= n <= 100000.0 <= roads.length <= 200000.- Travel times are positive.
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
- Cheapest Flights Within K StopsAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Path With Maximum ProbabilitySalesforce · ONSITE INTERVIEWSalesforce interview questions
- Dijkstra's Shortest PathsMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Single-Source Shortest PathsRippling · ONSITE INTERVIEWRippling interview questions
- Minimum-Weight Path Through a MatrixMeta · ONSITE INTERVIEWMeta interview questions
- Hub Vertices Within a RadiusThe Walt Disney Company · ONSITE INTERVIEWThe Walt Disney Company interview questions
- Minimum Path Cost with Edge DiscountsSkyscanner · ONSITE INTERVIEWSkyscanner interview questions
- Shortest Travel Time in a Grid with Traffic LightsWaymo · PHONE SCREENWaymo interview questions
- Shortest Paths in a Nonnegative Directed GraphNation with NaMo · PHONE SCREEN
- Dijkstra Shortest Paths in a Weighted Undirected GraphGoldman Sachs · PHONE SCREENGoldman Sachs interview questions