FastPrepShortest Directed Cycle Through a Start Node
Problems/Shortest Directed Cycle Through a Start Node

Shortest Directed Cycle Through a Start Node

Google logoGoogle● MediumNEW GRADONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: The report gives a directed graph, a chosen node, and the goal of the shortest cycle that returns to that node. It covers BFS, a discussion of parent paths, an illustration of an edge, and self-loops. We chose integer node numbers, returning the cycle length instead of the path, -1 when there is none, the examples, and the input limits. Our version is about 91% the same as the reported question.

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) → int

Examples

Example 1

nodeCount = 4edges = [[0,1],[1,2],[2,0],[1,3]]start = 0return = 3

Cycle 0→1→2→0 has three edges.

Example 2

nodeCount = 3edges = [[0,0],[0,1],[1,0]]start = 0return = 1

The self-loop is shorter than cycle 0→1→0.

Example 3

nodeCount = 4edges = [[0,1],[1,2],[2,3]]start = 0return = -1

No 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

Interview reports that mention this question

  • Google New Grad Four-Round Coding Onsite: 1 Experience

Google interview experiences

  • Google interviews · November 2026
  • Google Software Engineer Onsite Questions: 10 Experiences
  • Google OA: What It Tests (2026)
  • Google L4 SWE Phone Screen: 1 Experience
  • Google ML Engineering Manager Onsite: 1 Experience
All Google interview questionsSee Google hiring insights
Saved
class Solution {
    public int shortestCycleFromStart(int nodeCount, int[][] edges, int start) {
        // write your code here
    }
}
Checking account…