PracHub
QuestionsPremiumLearningGuidesCheatsheetNEWCoaches
|Home/Coding & Algorithms/MathWorks

Minimize shortest path by adding weight-1 edges

Last updated: Mar 29, 2026

Quick Overview

This question evaluates proficiency in graph algorithms, shortest-path computation, and algorithmic optimization when augmenting a directed weighted graph with additional unit-weight edges.

  • easy
  • MathWorks
  • Coding & Algorithms
  • Software Engineer

Minimize shortest path by adding weight-1 edges

Company: MathWorks

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Technical Screen

You are given a weighted directed graph with nodes labeled `1..n`. The existing edges are described by three arrays of equal length `m`: - `from[i]`: start node of edge `i` - `to[i]`: end node of edge `i` - `weight[i]`: positive weight of edge `i` You are allowed to **add any number of additional directed edges**, where **each added edge has weight = 1**, and you may choose its endpoints (any nodes in `1..n`). After adding edges optimally, compute the **minimum possible** shortest-path distance from node `1` to node `n`. **Input** - `n`: number of nodes - `from[0..m-1]`, `to[0..m-1]`, `weight[0..m-1]` **Output** - An integer: the smallest achievable distance from `1` to `n` after adding weight-1 edges. **Notes / Constraints (reasonable for interviews)** - `1 <= n <= 2e5` - `1 <= m <= 2e5` - `1 <= weight[i] <= 1e9`

Quick Answer: This question evaluates proficiency in graph algorithms, shortest-path computation, and algorithmic optimization when augmenting a directed weighted graph with additional unit-weight edges.

Related Interview Questions

  • Maximize minimum after K decrements - MathWorks (easy)
  • How to maximize rewards with exactly k tasks - MathWorks (easy)
  • Maximize minimum value after k decrements - MathWorks (medium)
  • Determine Whether P's Position Is Unique - MathWorks (medium)
  • Minimize reduction cost and validate equal-sum pairs - MathWorks (medium)
MathWorks logo
MathWorks
Mar 1, 2026, 12:00 AM
Software Engineer
Technical Screen
Coding & Algorithms
2
0

You are given a weighted directed graph with nodes labeled 1..n.

The existing edges are described by three arrays of equal length m:

  • from[i] : start node of edge i
  • to[i] : end node of edge i
  • weight[i] : positive weight of edge i

You are allowed to add any number of additional directed edges, where each added edge has weight = 1, and you may choose its endpoints (any nodes in 1..n).

After adding edges optimally, compute the minimum possible shortest-path distance from node 1 to node n.

Input

  • n : number of nodes
  • from[0..m-1] , to[0..m-1] , weight[0..m-1]

Output

  • An integer: the smallest achievable distance from 1 to n after adding weight-1 edges.

Notes / Constraints (reasonable for interviews)

  • 1 <= n <= 2e5
  • 1 <= m <= 2e5
  • 1 <= weight[i] <= 1e9

Comments (0)

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More MathWorks•More Software Engineer•MathWorks Software Engineer•MathWorks 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.