Server Network Distances with a Failure Radius
Updated
Problem statement
A network has n servers numbered 0 to n - 1. Each connections[i] = [u, v] is a two-way link, and sending a message across one link takes one hop.
Some servers have failed, listed in failed. A failure also takes down every server whose shortest hop distance to a failed server is at most radius, measured in the full network. These servers are disabled. Servers taken down this way do not spread the failure any further.
A message from start can pass only through servers that are not disabled. Return every server the message can reach, including start itself, as pairs [server, hops], where hops is the fewest hops needed. Sort the pairs by server number. Leave out disabled servers and servers that cannot be reached. If start is disabled, return an empty array.
When failed is empty, each pair gives the shortest hop count from start to that server, and a server with no pair cannot be reached from start at all.
Function
serverDistances(n: int, connections: int[][], start: int, failed: int[], radius: int) → int[][]Examples
Example 1
n = 6connections = [[0,1],[1,2],[2,3],[0,4],[4,3],[3,5]]start = 0failed = []radius = 0return = [[0,0],[1,1],[2,2],[3,2],[4,1],[5,3]]No server has failed. A breadth-first search from server 0 reaches 1 and 4 in one hop, 2 and 3 in two hops (3 through 4), and 5 in three hops. Every server is listed with its hop count.
Example 2
n = 6connections = [[0,1],[1,2],[2,3],[0,4],[4,3],[3,5]]start = 0failed = [4]radius = 0return = [[0,0],[1,1],[2,2],[3,3],[5,4]]Server 4 has failed and radius 0 disables only server 4. Messages must now go 0, 1, 2, 3, so server 3 is three hops away and server 5 is four.
Example 3
n = 7connections = [[0,1],[1,2],[2,3],[3,4],[4,5],[0,6]]start = 0failed = [3]radius = 1return = [[0,0],[1,1],[6,1]]Server 3 fails with radius 1, so servers 2, 3 and 4 are disabled. From server 0, only servers 1 and 6 can still be reached; server 5 is cut off.
Constraints
1 <= n <= 20000 <= connections.length <= 4000connections[i] = [u, v]with0 <= u, v < nandu != v; no pair of servers is linked twice.0 <= start < n0 <= failed.length <= n; the values infailedare distinct and each is in[0, n - 1].0 <= radius <= n
More Google Phone Screen questions
- Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Periodic-Color Binary Forest RootsPHONE SCREEN · Seen Nov 2026
- Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Best Stock Trade with Buy and Sell DaysPHONE SCREEN · Seen Nov 2026
- Word Chain Jumps in a GridPHONE SCREEN · Seen Nov 2026
- Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Apartment Housemate PreferencesPHONE SCREEN · Seen Nov 2026
- Price at Query TimestampsPHONE SCREEN · Seen Nov 2026
- Course Schedule Cycle DetectionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Course ScheduleAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Linked Merchants Within Two HopsStripe · PHONE SCREENStripe interview questions
- Shortest Pin Path Through TopicsPinterest · PHONE SCREENPinterest interview questions
- Shortest Travel Time in a Grid with Traffic LightsWaymo · PHONE SCREENWaymo interview questions
- Evaluate DivisionApple · PHONE SCREENApple interview questions
- Shortest Word Chain Between Two WordsFanatics · PHONE SCREEN
- Minimize CommuteDatabricks · OA · PHONE SCREENDatabricks interview questions
- Crawl Same-Hostname URLs ConcurrentlyAnthropic · PHONE SCREENAnthropic interview questions
- Possible BipartitionByteDance · PHONE SCREENByteDance interview questions
- Route Graph Reachability with a Broken LinkFigma · PHONE SCREENFigma interview questions