Back to Practice
#0065

Network Delay Time

HardDSA35 min25 XP

Problem

Given directed weighted edges times = [u, v, w], number of nodes n labeled 1..n, and start node k, return the time for all nodes to receive the signal. Return -1 if any node is unreachable.

Why This Matters

Shortest path is the foundation for routing, dependency costs, latency reasoning, and many graph interviews.

Function Signature

def network_delay_time(times, n, k):

Examples

Example 1
Inputtimes = [[2,1,1],[2,3,1],[3,4,1]], n = 4, k = 2
Output2

Node 4 receives the signal after 2 time units through 2 -> 3 -> 4.

Constraints

  • Edge weights are non-negative.
  • Nodes are labeled from 1 to n.
  • Return -1 if any node is unreachable.
CodePython
Visible browser tests run here when available.
Testcases1 visible / 3 hidden categories
Reach all nodes
Input[[2,1,1],[2,3,1],[3,4,1]], 4, 2
Expected2

The slowest shortest path from 2 takes two units.

Hidden Test Categories
Unreachable nodeMultiple paths to same nodeSingle-node graph