PracHub
QuestionsPremiumLearningGuidesInterview PrepNEWCoaches
|Home/Coding & Algorithms/Microsoft

Compute shortest path in cyclic graph

Last updated: Mar 29, 2026

Quick Overview

This question evaluates understanding of graph algorithms and shortest-path computation in weighted directed graphs, including handling cycles, non-negative edge weights, self-loops, and related edge-case reasoning.

  • medium
  • Microsoft
  • Coding & Algorithms
  • Software Engineer

Compute shortest path in cyclic graph

Company: Microsoft

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

## Problem: Shortest Path in a Graph With Cycles You are given a directed weighted graph that may contain cycles. - There are `n` nodes labeled `0..n-1`. - You are given an edge list `edges`, where each edge is `(u, v, w)` meaning there is a directed edge from `u` to `v` with non-negative weight `w`. - You are also given a source node `s` and a target node `t`. ### Task Return the length of the shortest path from `s` to `t`. If `t` is not reachable from `s`, return `-1`. ### Input/Output - **Input:** `n`, `edges`, `s`, `t` - **Output:** shortest distance from `s` to `t` or `-1` ### Constraints (assume for interview) - `1 <= n <= 2 * 10^5` - `0 <= |edges| <= 3 * 10^5` - `0 <= w <= 10^9` - Graph can contain self-loops and cycles ### Example - `n = 5` - `edges = [(0,1,2),(1,2,3),(2,1,1),(2,3,4),(0,4,10)]` - `s = 0, t = 3` Shortest path: `0 -> 1 -> 2 -> 3` with total weight `2 + 3 + 4 = 9`. So output is `9`.

Quick Answer: This question evaluates understanding of graph algorithms and shortest-path computation in weighted directed graphs, including handling cycles, non-negative edge weights, self-loops, and related edge-case reasoning.

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
Jan 3, 2026, 12:00 AM
Software Engineer
Technical Screen
Coding & Algorithms
1
0
Loading...

Problem: Shortest Path in a Graph With Cycles

You are given a directed weighted graph that may contain cycles.

  • There are n nodes labeled 0..n-1 .
  • You are given an edge list edges , where each edge is (u, v, w) meaning there is a directed edge from u to v with non-negative weight w .
  • You are also given a source node s and a target node t .

Task

Return the length of the shortest path from s to t. If t is not reachable from s, return -1.

Input/Output

  • Input: n , edges , s , t
  • Output: shortest distance from s to t or -1

Constraints (assume for interview)

  • 1 <= n <= 2 * 10^5
  • 0 <= |edges| <= 3 * 10^5
  • 0 <= w <= 10^9
  • Graph can contain self-loops and cycles

Example

  • n = 5
  • edges = [(0,1,2),(1,2,3),(2,1,1),(2,3,4),(0,4,10)]
  • s = 0, t = 3

Shortest path: 0 -> 1 -> 2 -> 3 with total weight 2 + 3 + 4 = 9. So output is 9.

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.