PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep
|Home/Coding & Algorithms/Microsoft

Find minimum cycle cost per node

Last updated: Mar 29, 2026

Quick Overview

This question evaluates proficiency in graph algorithms and algorithmic optimization, specifically reasoning about weighted directed graphs, cycle costs, and shortest-path computations.

  • hard
  • Microsoft
  • Coding & Algorithms
  • Software Engineer

Find minimum cycle cost per node

Company: Microsoft

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Take-home Project

## Problem You are given a **weighted directed graph** with `n` nodes labeled `0..n-1` and `m` directed edges. Each edge is `(u, v, w)` meaning you can go from `u` to `v` with cost `w`. For **each node `s`**, compute the **minimum total weight** of any path that: - starts at `s`, - follows directed edges, - and eventually returns to `s` (i.e., forms a directed cycle starting/ending at `s`). If it is **impossible** to return to `s`, output `0` for that node. ### Input - Integers `n, m` - `m` edges `(u, v, w)` ### Output - An array `res` of length `n`, where `res[s]` is the minimum cycle cost starting/ending at `s`, or `0` if none exists. ### Constraints (typical for an OA) - `1 <= n <= 2e5` - `0 <= m <= 2e5` - `1 <= w <= 1e9` ### Example If edges are: `(0,1,5)`, `(1,0,2)`, `(1,2,1)`, `(2,1,1)` then: - For `s=0`, min cycle is `0->1->0` cost `7`. - For `s=2`, min cycle is `2->1->2` cost `2`.

Quick Answer: This question evaluates proficiency in graph algorithms and algorithmic optimization, specifically reasoning about weighted directed graphs, cycle costs, and shortest-path computations.

Related Interview Questions

  • Return Top K Open Businesses - Microsoft (hard)
  • Implement Memory Allocation and In-Memory Records - Microsoft (medium)
  • Implement K-Means and Detect Divisible Subarrays - Microsoft (medium)
  • Sort Three Categories In Place - Microsoft (medium)
  • Retain Top K Elements - Microsoft (medium)
Microsoft logo
Microsoft
Feb 11, 2026, 12:00 AM
Software Engineer
Take-home Project
Coding & Algorithms
5
0
Loading...

Problem

You are given a weighted directed graph with n nodes labeled 0..n-1 and m directed edges. Each edge is (u, v, w) meaning you can go from u to v with cost w.

For each node s, compute the minimum total weight of any path that:

  • starts at s ,
  • follows directed edges,
  • and eventually returns to s (i.e., forms a directed cycle starting/ending at s ).

If it is impossible to return to s, output 0 for that node.

Input

  • Integers n, m
  • m edges (u, v, w)

Output

  • An array res of length n , where res[s] is the minimum cycle cost starting/ending at s , or 0 if none exists.

Constraints (typical for an OA)

  • 1 <= n <= 2e5
  • 0 <= m <= 2e5
  • 1 <= w <= 1e9

Example

If edges are: (0,1,5), (1,0,2), (1,2,1), (2,1,1) then:

  • For s=0 , min cycle is 0->1->0 cost 7 .
  • For s=2 , min cycle is 2->1->2 cost 2 .

Submit Your Answer to Earn 20XP

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 8,000+ 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.