FastPrepDistances to Cycles
Problems/Distances to Cycles

Distances to Cycles

Belvedere Trading logoBelvedere Trading● MediumFULLTIMEOA

Updated Oct 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: The distance-to-cycle objective matches the report at about 80%. The report omits the graph family and bounds; this exercise assumes a simple undirected graph with exactly one cycle per component.

You are given an undirected graph with vertices 0 through n - 1. Every connected component contains exactly one simple cycle.

For each vertex, return the minimum number of edges needed to reach any vertex on the unique cycle in its connected component. A vertex on a cycle has distance 0.

Function

distancesToCycles(n: int, edges: int[][]) → int[]

Examples

Example 1

n = 6edges = [[0,1],[1,2],[2,0],[2,3],[3,4],[4,5]]return = [0,0,0,1,2,3]

Vertices 0, 1, and 2 form the cycle. Vertices 3, 4, and 5 are respectively one, two, and three edges from it.

Example 2

n = 7edges = [[0,1],[1,2],[2,0],[3,4],[4,5],[5,3],[5,6]]return = [0,0,0,0,0,0,1]

The graph has two components. Vertices 0, 1, 2 and vertices 3, 4, 5 are on cycles; vertex 6 is one edge from the second cycle.

Constraints

  • 3 <= n <= 2 * 10^5.
  • edges.length = n.
  • Each edge contains two distinct vertices in [0, n - 1].
  • The graph has no duplicate edges.
  • Every connected component contains exactly one simple cycle.

More Belvedere Trading Online Assessment questions

  • Daily Settlement for Long and Short PositionsOA · Seen Oct 2026
  • Minimum AmountOA · Seen Oct 2026
  • Realized PnL with Oldest Inventory FirstOA · Seen Oct 2026

Similar questions at other companies

  • Cogwheel Rotation DirectionsOpenAI · OAOpenAI interview questions
  • Course Schedule IIChainalysis · OAChainalysis interview questions
  • Can You Finish All Courses?Intuit · OAIntuit interview questions
  • Course ScheduleInMobi · OAInMobi interview questions
  • Shortest Path in an Unweighted GraphCitadel · OACitadel interview questions
  • Course ScheduleAmazon · PHONE SCREEN · ONSITE INTERVIEW
  • Course ScheduleOracle · ONSITE INTERVIEWOracle interview questions
  • Course SchedulePlaud · ONSITE INTERVIEW
  • Find a Valid Course Completion OrderGoogle · ONSITE INTERVIEWGoogle interview questions
  • Course ScheduleByteDance · PHONE SCREENByteDance interview questions
See Belvedere Trading hiring insights
Saved
class Solution {
    public int[] distancesToCycles(int n, int[][] edges) {
      // write your code here
    }
}
Checking account…