Shortest Directed Cycle Through a Start Node
Updated
Problem statement
A directed graph has nodeCount nodes numbered from 0 through nodeCount - 1 and directed edges [from, to].
Given start, return the number of edges in the shortest directed cycle that begins at start and returns to start. Return -1 when no such cycle exists.
A self-loop [start, start] is a cycle of length 1.
Function
shortestCycleFromStart(nodeCount: int, edges: int[][], start: int) → intExamples
Example 1
nodeCount = 4edges = [[0,1],[1,2],[2,0],[1,3]]start = 0return = 3Cycle 0→1→2→0 has three edges.
Example 2
nodeCount = 3edges = [[0,0],[0,1],[1,0]]start = 0return = 1The self-loop is shorter than cycle 0→1→0.
Example 3
nodeCount = 4edges = [[0,1],[1,2],[2,3]]start = 0return = -1No directed path returns to node 0.
Constraints
1 <= nodeCount <= 100000.0 <= edges.length <= 200000.0 <= from, to, start < nodeCount.- Duplicate directed edges may appear.
More Google Onsite Interview questions
- Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Distinct Subset SumsONSITE INTERVIEW · Seen Nov 2026
- Playlist With No Same Artist in a RowONSITE INTERVIEW · Seen Nov 2026
- Delete Nodes and Return Forest RootsONSITE INTERVIEW · Seen Nov 2026
- Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- First and Last Target Position in a Mountain ArrayONSITE INTERVIEW · Seen Nov 2026
- Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- 0/1 Knapsack with Weight and Volume LimitsONSITE INTERVIEW · Seen Nov 2026
- Batched Subsequence Word MatchesONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Course ScheduleAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Course ScheduleOracle · ONSITE INTERVIEWOracle interview questions
- Course SchedulePlaud · ONSITE INTERVIEW
- Explore an Unknown Grid and Find a PathLinkedIn · ONSITE INTERVIEWLinkedIn interview questions
- Nearest Charging Station on a GridSpotify · ONSITE INTERVIEW
- Hierarchical Permission CheckeBay · ONSITE INTERVIEWeBay interview questions
- Evaluate DivisionPhonePe · ONSITE INTERVIEWPhonePe interview questions
- Count Friend CirclesOpenAI · PHONE SCREEN · ONSITE INTERVIEWOpenAI interview questions
- Finite Web CrawlerMongoDB · PHONE SCREEN · ONSITE INTERVIEWMongoDB interview questions
- Amount of Time to Infect a TreeMicrosoft · ONSITE INTERVIEWMicrosoft interview questions