FastPrepFastest Favorite City Through a Required Vertex
Problems/Fastest Favorite City Through a Required Vertex

Fastest Favorite City Through a Required Vertex

Google logoGoogle● MediumFULLTIMEONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: The report gives the task and the interview setting. We rebuilt the function signature, the tie-breaking rule, the examples, and the input limits to match the report as closely as we could. Our version is about 90% the same as the reported question.

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) → int

Examples

Example 1

n = 5roads = [[0,1,2],[1,2,2],[0,3,10],[2,4,1]]favorites = [3,4]source = 0required = -1return = 4

Favorite 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 = 4

Requiring city 3 makes favorite 4 the best.

Example 3

n = 3roads = [[0,1,2]]favorites = [2]source = 0required = -1return = -1

The 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

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 fastestFavoriteCity(int n, int[][] roads, int[] favorites, int source, int required) {
        // write your code here
    }
}
Checking account…