HARDasked at 1 company

Distance to a Cycle in Undirected Graph

A hard-tier problem at 73% community acceptance, tagged with Depth-First Search, Breadth-First Search, Union Find. Reported in interviews at Akuna Capital and 0 others.

Founder's read

You're asked to find the shortest distance from each node to the nearest cycle in an undirected graph. This problem hits the sweet spot between graph traversal and cycle detection where the naive approach collapses. It's asked at trading firms and quant shops that care about your ability to reason about graph structure under pressure. The acceptance rate sits at 73%, which sounds high until you realize most submissions fail on edge cases around disconnected components or nodes already in a cycle. If this problem lands in your live OA and you freeze on the approach, StealthCoder runs invisibly and surfaces a working solution in seconds.

Companies asking
1
Difficulty
HARD
Acceptance
73%

Companies that ask "Distance to a Cycle in Undirected Graph"

If this hits your live OA

Distance to a Cycle in Undirected Graph is the kind of problem that decides whether you pass. StealthCoder reads the problem on screen and surfaces a working solution in under 2 seconds. Invisible to screen share. The proctor sees nothing. Made for the engineer who has done the work but might still blank with a webcam pointed at him.

Get StealthCoder
What this means

The trick is recognizing that you can't just run DFS or BFS from every node independently. Instead, you need to find all cycles first, then compute distances backwards from those cycles to every other node. Union Find efficiently identifies cycle membership, then multi-source BFS from all cycle nodes gives you the minimum distance to any cycle in one pass. The pitfall most candidates hit: trying to track distances during cycle detection itself, which scrambles your logic. Another common miss: forgetting that nodes already in a cycle have distance zero. The problem demands you separate concerns cleanly across two phases. If you haven't drilled this specific pattern before and hit it live, StealthCoder's approach bypasses the trial-and-error that kills time on assessments.

Pattern tags

The honest play

You know the problem. Make sure you actually pass it.

Distance to a Cycle in Undirected Graph recycles across companies for a reason. It's hard-tier, and most candidates blank under the timer. StealthCoder is the hedge: an AI overlay invisible during screen share. It reads the problem and surfaces a working solution in under 2 seconds. Made for the engineer who has done the work but might still blank with a webcam pointed at him. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Distance to a Cycle in Undirected Graph interview FAQ

Is this really a hard problem, or does the acceptance rate mean it's easier than it looks?+

The 73% acceptance is misleading. Most solvers know the two-phase pattern going in. Cold, you'll stumble on state management and multi-source BFS setup. The problem itself is Hard for a reason: the algorithmic leap from 'find cycles' to 'measure from cycles' isn't obvious on first read.

Why can't I just run BFS from every node and check if it hits a cycle?+

That's O(n^2) or worse and doesn't cleanly identify what counts as 'in a cycle.' You'd also recompute distances repeatedly. The correct approach finds all cycle nodes once with Union Find, then does one multi-source BFS. Much cleaner and faster.

How does Union Find help here if the graph has cycles?+

Union Find detects the exact moment an edge creates a cycle, marking both endpoints as cycle members. You iterate edges once, union as you go, and flag any edge whose endpoints are already connected. That's your cycle detection.

What if a node is already part of a cycle?+

Its distance is zero. Multi-source BFS seeds all cycle nodes as the starting layer. When BFS processes neighbors, distances increase naturally. Nodes in cycles never get visited from outside because they're already in the queue at step zero.

Does this work for disconnected graphs?+

Yes. Disconnected components without cycles will have nodes at distance infinity or unreachable. Your multi-source BFS will only reach nodes reachable from cycle nodes. Handle unreachable nodes explicitly if the problem requires it, usually by returning -1 or reporting them separately.

Want the actual problem statement? View "Distance to a Cycle in Undirected Graph" on LeetCode →

Frequency and company-tag data sourced from public community-maintained interview-report repos. Problem, description, and trademark © LeetCode. StealthCoder is not affiliated with LeetCode.