Find the City With the Smallest Number of Neighbors at a Threshold Distance
Medium00:00
Section 1Understand Problem
Section 01
Understand the problem
Restate the problem precisely. Interviewers reward candidates who pause, define the contract, and name the guarantees before coding.
Problem statement
There are `n` cities numbered from 0 to n-1. Given the array `edges` where `edges[i] = [from, to, weight]` represents a bidirectional weighted edge, and an integer `distanceThreshold`, return the city with the *smallest* number of cities that are reachable through some path with total distance at most `distanceThreshold`. If there is more than one such city, return the city with the *greatest* number.
Example
n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4 → 3
Constraints
2 ≤ n ≤ 100 · 1 ≤ edges.length ≤ n(n−1)/2 · edges[i].length = 3 · 0 ≤ from, to < n, from ≠ to · 1 ≤ weight, distanceThreshold ≤ 10⁴
Audio recording unsupported on this browser.
WPM—Fillers0Pace—Thinking0%
Explain the problem in your own words
0 words0 chars
AI Review
Write your thinking above first.
Once you've drafted a response, click Submit for AI review and a senior engineer will critique your reasoning.
Ready to submit Section 1?
The reviewer will check whether you stated inputs, output, guarantees, and any assumption you flagged.
Write at least a couple of sentences explaining the problem before submitting.