← Microsoft Interview Insights
I knew BFS was involved but fumbled the first step a bit.
First, identify the unique cycle in the undirected graph using degree-based pruning (topological sort). Then, perform a multi-source BFS from all cycle nodes to compute the shortest distance to the cycle for every node.
Pro tip: Mention that this approach runs in O(V+E) time and O(V) space, which is optimal. Also, clarify that the graph is connected and has exactly one cycle, so the cycle is unique and all nodes are reachable from it.
Confirm that the graph is undirected, connected, and contains exactly one cycle. The goal is to compute the shortest distance from each node to any node on the cycle.
Use a degree-based pruning approach (similar to topological sort): repeatedly remove nodes with degree 1 and update their neighbors' degrees. The remaining nodes with degree ≥2 form the cycle.
Initialize a queue with all cycle nodes (distance 0) and perform BFS. For each neighbor not yet visited, set its distance to current distance + 1 and enqueue it.
After BFS completes, the distance array contains the shortest distance from each node to the cycle. Nodes on the cycle have distance 0.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.