Skip to main content

24. Network Delay Time

mediumAsked at Doordash

Find the minimum time a signal takes to reach all nodes in a weighted graph — Doordash maps this directly to Dijkstra delivery-routing: how long until every zone gets its first Dasher signal.

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

Problem

You are given a network of n nodes labeled 1 to n, a list of travel times as directed edges times[i] = [u, v, w] (from node u to node v with weight w), and a source node k. Return the minimum time it takes for all n nodes to receive the signal sent from k. If it is impossible for all nodes to receive the signal, return -1.

Constraints

  • 1 <= k <= n <= 100
  • 1 <= times.length <= 6000
  • times[i].length == 3; 1 <= u_i, v_i <= n; 1 <= w_i <= 100
  • All (u_i, v_i) pairs are unique

Examples

Example 1

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

Explanation: Signal travels 2->1 (1ms), 2->3 (1ms), 3->4 (2ms total). Slowest is 4, at 2ms.

Example 2

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

Explanation: Node 1 is unreachable from node 2.

Approaches

1. Bellman-Ford (relax all edges n-1 times)

Initialize distances to Infinity, source to 0. Relax every edge n-1 times. Return max distance; -1 if any remain Infinity.

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 ans = Math.max(...dist.slice(1));
  return ans === Infinity ? -1 : ans;
}

Tradeoff:

2. Dijkstra with min-heap

Build adjacency list; use a min-heap (priority queue) to always expand the shortest-known-distance node first. Stops once all nodes settled.

Time
O((V + E) log V)
Space
O(V + 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;
  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 ans = Math.max(...dist.slice(1));
  return ans === Infinity ? -1 : ans;
}

Tradeoff:

Doordash-specific tips

Doordash explicitly asks Dijkstra variants because their routing engine finds the fastest Dasher path in a weighted city graph. Interviewers want you to articulate why Dijkstra beats Bellman-Ford here: no negative weights, sparse graph, need early termination. They'll follow up with: 'what if drive times change in real time?' — that's the cue to discuss Re-Dijkstra on delta updates or A* with GPS heuristics. Mention that their production system uses contracted graphs for sub-second routing.

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 Doordash interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.

Practice these live with InterviewChamp.AI →