Compute Minimum Broadcast Latencies Between Cities
Company: Netflix
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: easy
Interview Round: Technical Screen
## Problem
Cities are connected by bidirectional network links with positive ping times. Starting from one city, compute the minimum total ping needed to reach every city. Unreachable cities should be reported as `-1`.
### Function Contract
Implement `minimum_broadcast_latencies(n, links, source) -> list[int]`. Each link is `[u, v, ping]`; the returned list is indexed by city.
### Constraints
- `1 <= n <= 200000` and `0 <= len(links) <= 300000`.
- `1 <= ping <= 10^9`; parallel links are allowed.
- `0 <= source < n`.
- Distances can exceed a 32-bit signed integer.
### Examples
- For `n = 4`, links `[[0,1,5],[0,2,2],[2,1,1],[1,3,3]]`, and source `0`, return `[0,3,2,6]`.
- With no links and source `1` in a three-city graph, return `[-1,0,-1]`.
```hint Finalize the nearest frontier
A min-priority queue lets the smallest known unsettled distance become final before longer alternatives are expanded.
```
```hint Treat changing pings separately
If link weights update continuously, identify whether queries justify recomputing or require a dynamic shortest-path structure.
```
### Edge Cases
- The source city always has distance zero.
- The graph may be disconnected.
- A later path can improve a distance discovered through a direct link.
Quick Answer: Compute minimum broadcast latency from one city to every city in a weighted undirected network. Use Dijkstra's algorithm with wide integer distances, allow parallel links, ignore stale queue entries, and report unreachable cities as negative one.
Cities are connected by bidirectional network links with positive ping times. Given n cities indexed from 0 through n-1, a list of links [u, v, ping], and a source city, return the minimum total ping needed to reach every city. The returned list is indexed by city. Use -1 for each city that is unreachable from the source. Parallel links are allowed, and distances can exceed a signed 32-bit integer.
Constraints
- 1 <= n <= 200000.
- 0 <= len(links) <= 300000.
- Each link is [u, v, ping] with 0 <= u, v < n and 1 <= ping <= 1000000000.
- Links are bidirectional, and parallel links are allowed.
- 0 <= source < n.
- Distances can exceed a signed 32-bit integer.
Examples
Input: (4, [[0, 1, 5], [0, 2, 2], [2, 1, 1], [1, 3, 3]], 0)
Expected Output: [0, 3, 2, 6]
Explanation: The route through city 2 improves city 1 before city 3 is reached.
Input: (3, [], 1)
Expected Output: [-1, 0, -1]
Explanation: Only the source is reachable when the graph has no links.
Hints
- Finalize the smallest unsettled distance first with a min-priority queue.
- When a shorter route is found, push its new distance and ignore stale entries later.