Distances While Avoiding a Broken Node
Updated
Problem statement
You are given a directed unweighted graph with vertices 0 through n-1, a start vertex, and one broken vertex that cannot be visited.
For every vertex, return the fewest edges needed to reach it from start without visiting the broken vertex. Use -1 for unreachable vertices, including the broken vertex. If start is broken, return all -1.
Cycles and duplicate edges are allowed.
Function
distancesAvoidingBroken(n: int, edges: int[][], start: int, broken: int) → int[]Examples
Example 1
n = 5edges = [[0,1],[1,2],[0,3],[3,2],[2,4]]start = 0broken = 1return = [0,-1,2,1,3]Vertex 2 is reached through 0→3→2 because vertex 1 is unavailable.
Example 2
n = 3edges = [[0,1],[1,2],[2,0]]start = 0broken = 2return = [0,1,-1]The cycle is handled without revisiting vertices.
Example 3
n = 3edges = [[0,1]]start = 1broken = 1return = [-1,-1,-1]A broken start cannot reach any vertex.
Constraints
1 <= n <= 200000.0 <= edges.length <= 400000.- Every edge contains two valid vertex IDs.
startandbrokenare valid vertices.
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