Network Delay Time
Problem Statement
A network has n nodes numbered 1 to n. You are given times, a list of one-way links [u, v, w]: a signal sent from u reaches v after w time.
A signal starts at node k at time 0 and spreads along the links. Return the time when the last node receives it. If some node never receives it, return -1.
Write int networkDelayTime(vector<vector<int>>& times, int n, int k) inside class Solution.
times, the one-way links. n, the number of nodes. k, the start node.
The time at which the last node gets the signal, or -1 if some node never gets it.
Node 2 starts at time 0. Nodes 1 and 3 hear it at time 1, and node 4 hears it through node 3 at time 2. The last node hears it at time 2.
Node 2 hears the signal at time 1.
There is no link out of node 2, so node 1 never hears the signal.
- • 1 ≤ k ≤ n ≤ 100
- • 1 ≤ times.length ≤ 6000
- • times[i].length == 3
- • 1 ≤ u_i, v_i ≤ n
- • u_i ≠ v_i
- • 0 ≤ w_i ≤ 100
- • All the pairs (u_i, v_i) are unique.
Pattern Recognition Clues
Recognising these key signals in the problem description immediately identifies the pattern:
Approach Complexity Ladder
Relax every link n − 1 times, keeping the earliest time per node
Pop the earliest pending arrival; the largest final time is the answer
Start with time 0 at node k and INT_MAX everywhere else. Go through every link and, whenever a node can hear sooner through that link, lower its time. Do this for n − 1 full rounds. Then take the largest time, or -1 if a node is still unreached.
Recognition rule for this sub-pattern:
Unlock Complete Visual Walkthrough & Optimal C++
The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.
First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.
How this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like Shortest Paths (Weighted) translate into production architectures and low-level memory mechanics.
Foundations: Object-Oriented Principles in C++23
Class invariants, pure virtual interfaces, and RAII smart pointers.
Food Delivery System (Zomato Architecture)
Real-world machine coding application of algorithms and design patterns.
Pointers vs References Deep Dive
Pointer arithmetic and memory layout under the hood.
Virtual Method Table (vtable) & Memory Layout
Polymorphism, vptr overhead, and multiple inheritance in memory.