DSA interview question
A server cluster has n nodes labelled 1..n and directed links with transmission times. Node k broadcasts a signal. How long until every node has received it? Each node receives the signal at its shortest-path time from k, so the answer is the largest of those shortest times, or −1 if some node is unreachable. That's single-source shortest paths with non-negative weights: Dijkstra.
Example: times = [[2,1,1], [2,3,1], [3,4,1]], n = 4, k = 2 → 2.
int networkDelayTime(const vector<array<int, 3>>& times, int n, int k); // nodes 1..n; times {u, v, w}
Input (times, n, k) | Output | Explanation |
|---|---|---|
([[2,1,1],[2,3,1],[3,4,1]], 4, 2) | 2 | Node 4 is last, at time 2 |
([[1,2,1],[2,3,2],[1,3,4]], 3, 1) | 3 | 1→2→3 beats the direct 4 |
([[1,2,1]], 2, 1) | 1 | |
([[1,2,1]], 2, 2) | -1 | Node 1 can't be reached from 2 |
([], 1, 1) | 0 | Only the source |
(Confirm: links are directed; times are non-negative; the signal starts at time 0.)
On Practick another engineer interviews you live for 60 minutes on a question from this bank. They have the full interviewer guide beside the call: the expected answer to every follow-up, hints to give if you stall, and a scoring rubric. You get the model solution once the session settles. It is free: interview someone for an hour and you have earned your own.
Book a free mock interviewAll 474 dsa interview questions
DSA mock interviews · System design interview questions · Free mock interview practice · Browse interviewers · Sign in