The Find the City With the Smallest Number of Neighbors at a Threshold Distance interview question is a graph-based reachability problem. You are given a set of cities and weighted edges representing the distances between them. You are also given a distanceThreshold. Your goal is to find the city that can reach the smallest number of other cities within this threshold. If multiple cities tie for the smallest count, you return the one with the largest index.
Companies like Uber, Microsoft, and Google use the Find the City With the Smallest Number of Neighbors coding problem to test a candidate's mastery of "All-Pairs Shortest Path" algorithms. It evaluates your ability to handle weighted graphs and choose the most appropriate algorithm (like Floyd-Warshall or multiple Dijkstra runs) based on the problem constraints. It’s a core Graph interview pattern.
This problem is typically solved using the Floyd-Warshall Algorithm.
dist[n][n] initialized to infinity, with dist[i][i] = 0.k to find shorter paths between any two cities i and j: dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]).dist[i][j] <= distanceThreshold.Cities: 0, 1, 2. Threshold: 3. Edges: (0-1, weight 3), (1-2, weight 1).
dist[i][i] = 0 or using a value for "infinity" that is too small.Master the Floyd-Warshall algorithm for "All-Pairs" problems. While Dijkstra is better for "One-to-All," Floyd-Warshall's simplicity () makes it the preferred choice for dense graphs or when you need reachability info for every single node.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Number of Ways to Arrive at Destination | Medium | Solve | |
| The Most Similar Path in a Graph | Hard | Solve | |
| Number of Restricted Paths From First to Last Node | Medium | Solve | |
| Cheapest Flights Within K Stops | Medium | Solve | |
| Jump Game VIII | Medium | Solve |