PracHub
QuestionsPremiumLearningGuidesCheatsheetNEWCoaches
|Home/Coding & Algorithms/Microsoft

Compute distance of each node to a cycle

Last updated: Mar 29, 2026

Quick Overview

This question evaluates proficiency in graph algorithms, including understanding unicyclic graph structure, cycle identification, and computing minimum distances from nodes to a cycle, and belongs to the Coding & Algorithms domain within graph theory.

  • medium
  • Microsoft
  • Coding & Algorithms
  • Software Engineer

Compute distance of each node to a cycle

Company: Microsoft

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

## Problem You are given an **undirected** graph with `n` nodes labeled `0..n-1` and a list of edges. The graph is connected and contains **exactly one simple cycle** (i.e., it is a *unicyclic* graph). For each node `i`, return `dist[i]` = the **minimum number of edges** from node `i` to **any node on the cycle**. - Nodes that are on the cycle have distance `0`. ### Example (illustrative) If nodes `{2,3,4}` form the only cycle, then `dist[2]=dist[3]=dist[4]=0`, and nodes in trees attached to the cycle have distance equal to their shortest path length to any of `{2,3,4}`. ### Input / Output - Input: - integer `n` - edges: list of pairs `(u, v)` - Output: integer array `dist` of length `n` ### Constraints (assume) - `2 ≤ n ≤ 2 * 10^5` - `edges.length = n` (connected unicyclic graph) - `0 ≤ u, v < n`, `u != v` ### Notes A linear-time approach is expected. (A common approach involves pruning leaves to identify cycle nodes, then multi-source BFS from the cycle.)

Quick Answer: This question evaluates proficiency in graph algorithms, including understanding unicyclic graph structure, cycle identification, and computing minimum distances from nodes to a cycle, and belongs to the Coding & Algorithms domain within graph theory.

Related Interview Questions

  • Sort Three Categories In Place - Microsoft (medium)
  • Implement K-Means and Detect Divisible Subarrays - Microsoft (medium)
  • Implement SFT Sample Packing - Microsoft (medium)
  • Implement SQL Table and DNA Ordering - Microsoft (medium)
  • Solve power jumps and graph tour - Microsoft (hard)
Microsoft logo
Microsoft
Jan 9, 2026, 12:00 AM
Software Engineer
Onsite
Coding & Algorithms
4
0
Loading...

Problem

You are given an undirected graph with n nodes labeled 0..n-1 and a list of edges. The graph is connected and contains exactly one simple cycle (i.e., it is a unicyclic graph).

For each node i, return dist[i] = the minimum number of edges from node i to any node on the cycle.

  • Nodes that are on the cycle have distance 0 .

Example (illustrative)

If nodes {2,3,4} form the only cycle, then dist[2]=dist[3]=dist[4]=0, and nodes in trees attached to the cycle have distance equal to their shortest path length to any of {2,3,4}.

Input / Output

  • Input:
    • integer n
    • edges: list of pairs (u, v)
  • Output: integer array dist of length n

Constraints (assume)

  • 2 ≤ n ≤ 2 * 10^5
  • edges.length = n (connected unicyclic graph)
  • 0 ≤ u, v < n , u != v

Notes

A linear-time approach is expected. (A common approach involves pruning leaves to identify cycle nodes, then multi-source BFS from the cycle.)

Comments (0)

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More Microsoft•More Software Engineer•Microsoft Software Engineer•Microsoft Coding & Algorithms•Software Engineer Coding & Algorithms
PracHub

Master your tech interviews with 7,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.