FastPrepNearest Favorite Cities
Problems/Nearest Favorite Cities

Nearest Favorite Cities

Google logoGoogle● MediumNEW GRADONSITE INTERVIEW

Updated Oct 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: The firsthand Google new-grad report asks for every favorite city at the minimum shortest-path distance from a starting city and notes that ties require a list. The unweighted undirected map, sorted output, and unreachable convention are explicit practice assumptions.

An undirected, unweighted map contains n cities numbered from 0 to n - 1. Given the roads, a starting city, and a list of favorite cities, find the fewest roads needed to reach any favorite city. Return every favorite city that can be reached using that minimum number of roads.

Return the selected city IDs in increasing order. If no favorite city is reachable, return an empty array.

Function

nearestFavoriteCities(n: int, roads: int[][], start: int, favorites: int[]) → int[]

Examples

Example 1

n = 6roads = [[0,1],[1,2],[0,3],[3,4],[4,5]]start = 0favorites = [2,4,5]return = [2,4]

Cities two and four are both two roads away; city five is farther.

Example 2

n = 4roads = [[0,1],[1,2],[2,3]]start = 1favorites = [0,3]return = [0]

City zero is one edge away, while city three is two edges away.

Example 3

n = 5roads = [[0,1],[2,3]]start = 0favorites = [3,4]return = []

Neither favorite is reachable from city zero.

Constraints

  • 1 <= n <= 200000.
  • 0 <= roads.length <= 300000.
  • Each road contains two distinct valid city IDs.
  • start and every favorite are valid city IDs.
  • 1 <= favorites.length <= n, and favorites are unique.

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

  • Delivery Management SystemAkuna Capital · OAAkuna Capital interview questions
  • Cheapest Flights Within K StopsAmazon · PHONE SCREEN · ONSITE INTERVIEW
  • Fire Station Response BillLinkedIn · ONSITE INTERVIEWLinkedIn interview questions
  • Minimum Friend API CallsMercor · ONSITE INTERVIEWMercor interview questions
  • Shortest Increasing Path to a TargetUber · ONSITE INTERVIEWUber interview questions
  • Find and Reconstruct a Binary-Matrix PathMeta · PHONE SCREEN · ONSITE INTERVIEWMeta interview questions
  • Open the Lock and Return a Shortest PathZip · PHONE SCREEN · ONSITE INTERVIEWZip interview questions
  • Find All People With SecretIDFC Bank · ONSITE INTERVIEWIDFC Bank interview questions
  • Shortest Travel Time in a Grid with Traffic LightsWaymo · PHONE SCREENWaymo interview questions
  • Shortest Word Chain Between Two WordsFanatics · PHONE SCREEN

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[] nearestFavoriteCities(int n, int[][] roads, int start, int[] favorites) {
        // Write your code here.
    }
}
Checking account…