Find the City With the Smallest Number of Neighbors at a Threshold Distance
A medium-tier problem at 70% community acceptance, tagged with Dynamic Programming, Graph, Shortest Path. Reported in interviews at Expedia and 0 others.
You're solving a graph problem where you need to find which city has the fewest neighbors within a given distance threshold. Expedia has asked this one. The trap is thinking you can just run a single shortest-path algorithm and call it done. You need to compute shortest paths from every node, then count reachable neighbors for each. It's a 70% acceptance rate problem, so candidates are either nailing the approach or missing the scale of the computation required. If you blank on the multi-source setup during your live OA, StealthCoder surfaces the solution invisibly.
Companies that ask "Find the City With the Smallest Number of Neighbors at a Threshold Distance"
Find the City With the Smallest Number of Neighbors at a Threshold Distance is the kind of problem that decides whether you pass. StealthCoder reads the problem on screen and surfaces a working solution in under 2 seconds. Invisible to screen share. The proctor sees nothing. Made by an Amazon engineer who watched the leaked-problem repo become an industry secret. He decided you should have it too.
Get StealthCoderThe core pattern here is all-pairs shortest paths. Floyd-Warshall or running Dijkstra from each node both work, but the choice matters for time complexity on larger graphs. Once you have the distance matrix, the algorithm is trivial: for each city, count how many others fall within the threshold distance. The common mistake is running one shortest-path query and trying to answer the question from that. Another pitfall is not handling tie-breaking correctly if multiple cities have the same minimum neighbor count. The Dynamic Programming angle comes from Floyd-Warshall's relaxation logic. This is where StealthCoder becomes a safety net if you freeze on which shortest-path strategy fits the constraints during the assessment.
Pattern tags
You know the problem.
Make sure you actually pass it.
Find the City With the Smallest Number of Neighbors at a Threshold Distance recycles across companies for a reason. It's medium-tier, and most candidates blank under the timer. StealthCoder is the hedge: an AI overlay invisible during screen share. It reads the problem and surfaces a working solution in under 2 seconds. Made by an Amazon engineer who watched the leaked-problem repo become an industry secret. He decided you should have it too. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Find the City With the Smallest Number of Neighbors at a Threshold Distance interview FAQ
Is this really just implementing Floyd-Warshall and counting neighbors?+
Essentially yes, but the implementation matters. You build the all-pairs distance table, then iterate each city and count neighbors under the threshold. Return the city with the lowest count. Tie-breaking goes to the largest city index. It's straightforward once you commit to the all-pairs approach.
Why is the acceptance rate so high at 70%?+
The algorithm is not hard to understand once you see it. Most candidates who recognize the all-pairs pattern and code it cleanly pass. Failures usually come from off-by-one errors, forgetting to initialize the distance matrix, or returning the wrong city when there's a tie.
How does this relate to the Dynamic Programming topic?+
Floyd-Warshall uses DP to build the distance table by relaxing paths through intermediate nodes. Each entry represents the optimal subproblem solution. It's textbook DP, though some solutions use repeated Dijkstra calls instead, which is still valid but different in structure.
Dijkstra or Floyd-Warshall, which should I pick?+
Depends on graph density and constraints. Floyd-Warshall is O(n^3) always. Running Dijkstra n times is O(n log n * n) with a heap. For dense graphs, Floyd-Warshall wins. For sparse graphs, repeated Dijkstra often does. Both are acceptable solutions here.
Will Expedia ask follow-up optimizations if I solve it?+
Possibly. They might ask how you'd handle very large graphs, or whether you can optimize space. Knowing your trade-offs between Floyd-Warshall and repeated Dijkstra matters. Also be ready to explain why the naive single-source approach fails.
Want the actual problem statement? View "Find the City With the Smallest Number of Neighbors at a Threshold Distance" on LeetCode →