Skip to main content

27. Network Delay Time

mediumAsked at Coinbase

Find the worst-case latency before a broadcast reaches every node — Coinbase uses Dijkstra problems to evaluate how engineers reason about minimum-cost routing across crypto-network relay nodes and cross-chain bridging hops.

By Alex Chen, Founder, InterviewChamp.AI · Last verified

Problem

You are given a network of n nodes labelled 1 to n and a list of directed travel-time edges times[i] = [ui, vi, wi]. A signal is sent from node k. Return the minimum time for all n nodes to receive the signal. If it is impossible, return -1.

Constraints

  • 1 <= k <= n <= 100
  • 1 <= times.length <= 6000
  • times[i].length == 3
  • 1 <= wi <= 100

Examples

Example 1

Input
times = [[2,1,1],[2,3,1],[3,4,1]], n = 4, k = 2
Output
2

Explanation: Signal reaches 1 in 1, reaches 3 in 1, reaches 4 via 3 in 2. Max = 2.

Example 2

Input
times = [[1,2,1]], n = 2, k = 2
Output
-1

Approaches

1. Bellman-Ford

Relax all edges n-1 times. Works for any edge weights (including negative). O(V*E).

Time
O(n * E)
Space
O(n)
function networkDelayTime(times, n, k) {
  const dist = new Array(n + 1).fill(Infinity);
  dist[k] = 0;
  for (let i = 0; i < n - 1; i++) {
    for (const [u, v, w] of times) {
      if (dist[u] !== Infinity && dist[u] + w < dist[v]) {
        dist[v] = dist[u] + w;
      }
    }
  }
  const max = Math.max(...dist.slice(1));
  return max === Infinity ? -1 : max;
}

Tradeoff:

2. Dijkstra with min-heap (optimal for non-negative weights)

Priority queue always expands the nearest unvisited node. O((V + E) log V) — the standard for sparse, non-negative-weight graphs.

Time
O((n + E) log n)
Space
O(n + E)
function networkDelayTime(times, n, k) {
  const graph = Array.from({ length: n + 1 }, () => []);
  for (const [u, v, w] of times) graph[u].push([v, w]);

  const dist = new Array(n + 1).fill(Infinity);
  dist[k] = 0;
  // MinHeap simulation via sorted array (acceptable for n<=100)
  const heap = [[0, k]];

  while (heap.length) {
    heap.sort((a, b) => a[0] - b[0]);
    const [d, u] = heap.shift();
    if (d > dist[u]) continue;
    for (const [v, w] of graph[u]) {
      if (dist[u] + w < dist[v]) {
        dist[v] = dist[u] + w;
        heap.push([dist[v], v]);
      }
    }
  }

  const max = Math.max(...dist.slice(1));
  return max === Infinity ? -1 : max;
}

Tradeoff:

Coinbase-specific tips

Coinbase frames this as broadcast-delay in their peer-to-peer validator network: given latency weights on relay hops, how long before every node has the new block header? They expect you to choose Dijkstra for non-negative weights and to explain why Bellman-Ford is unnecessary here — but know it for the follow-up where negative latency adjustments (fee rebates) are introduced.

Solve it now

Free. No sign-up. Python and JavaScript run instantly in your browser.

Output

Press Run or Cmd+Enter to execute

Practice these live with InterviewChamp.AI

Drill Network Delay Time and other Coinbase interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.

Practice these live with InterviewChamp.AI →