L3 · FAANGGraphs~20 min · 5 tests

Network Delay Time (Dijkstra)

Find how long a signal takes to reach every node of a weighted graph with Dijkstra's algorithm and heapq in O(E log V). Return -1 if a node is unreachable.

The problem

times is a list of directed edges [u, v, w] (travel time w ≥ 0) between nodes 1..n. A signal starts at node k. Return the time until all nodes have received it, or -1 if some node never does.

Examples

  1. Example 1

    Input

    network_delay([[2, 1, 1], [2, 3, 1], [3, 4, 1]], 4, 2)

    Expected output

    2
  2. Example 2

    Input

    network_delay([[1, 2, 1]], 2, 2)

    Expected output

    -1

+ 3 hidden tests on Submit — indirect path is shorter, zero-weight edge.

Edge cases to ask about

  • Unreachable node
  • Single node
  • Zero-weight edges

Hints

0/3

    How an interviewer scores this

    0/9
    Python 3.13 · network_delay
    ⌘/Ctrl + Enter runs the examples

    Your code runs in real CPython inside your browser — nothing is sent anywhere. The first run downloads the interpreter (about 6 MB, once). Your code is saved on this device as you type.

    Complexity Lab

    What does this cost as n grows?

    Interviewers score the analysis as much as the code. Commit to an answer first — then check it, and measure your code against the optimal one at growing input sizes.

    Time complexity of the optimal solution
    Space complexity (extra memory)

    Pick both to reveal the answer.

    Measure it

    Runs the function on inputs of size 250 up to 16,000 and records the time and peak memory. Slow solutions stop early — a short curve is itself the answer.

    From brute force to optimal

    The progression an interviewer wants to hear, one step at a time.

    ApproachTimeSpaceIdea
    Bellman-FordO(V · E)O(V)Handles negative weights; not needed here.
    bestDijkstra with a binary heapO(E log V)O(V + E)Pop the closest unsettled node; its distance is final.
    Walkthrough of the optimal approach (try it yourself first)

    Dijkstra: keep a min-heap of (distance, node). Pop the closest; if it's already settled, skip it (lazy deletion). Otherwise its distance is final — record it and push its neighbours with d + w. The answer is the largest settled distance, or -1 if not every node was reached.

    Dijkstra needs non-negative weights; with negative edges use Bellman-Ford.

    Complexity: O(E log V) time, O(V + E) space. Every edge may push one heap entry, and each push/pop costs O(log E) = O(log V).

    Reveal the reference solution
    import heapq
    
    def network_delay(times, n, k):
        graph = {i: [] for i in range(1, n + 1)}
        for u, v, w in times:
            graph[u].append((v, w))
        dist = {}
        heap = [(0, k)]
        while heap:
            d, node = heapq.heappop(heap)
            if node in dist:
                continue
            dist[node] = d
            for nxt, w in graph[node]:
                if nxt not in dist:
                    heapq.heappush(heap, (d + w, nxt))
        return max(dist.values()) if len(dist) == n else -1

    Follow-ups interviewers ask

    • Return the path, not just the time.
    • Cheapest flight with at most k stops (modified BFS/Bellman-Ford).

    Frequently asked interview questions

    Core interview concepts, complexities, and follow-ups scored by hiring teams.

    What is the time complexity of Network Delay Time (Dijkstra) in Python?

    The optimal solution runs in O(E log V) time and O(V + E) auxiliary space. Every edge may push one heap entry, and each push/pop costs O(log E) = O(log V).

    What is the brute-force approach, and how do you optimise it?

    Bellman-Ford: O(V · E) time, O(V) space. Handles negative weights; not needed here. Dijkstra with a binary heap: O(E log V) time, O(V + E) space. Pop the closest unsettled node; its distance is final.

    What follow-up questions do interviewers ask about Network Delay Time (Dijkstra)?

    Return the path, not just the time. Cheapest flight with at most k stops (modified BFS/Bellman-Ford).