As reported: Amazon SDE I interview (university hiring, Bengaluru, June 2025), round 1 — one hour, about ten minutes of introductions and then two DSA questions; this "Optimal Network Construction" problem was the first and took about 40 minutes. The twist: not a single minimum spanning tree, but every road that can be part of one.
There are m cities 0 … m−1 and n roads; roads[i] = [u, v, toll] connects cities u and v with toll tax toll.
An optimal network is a set of roads that connects all cities, uses the minimum number of roads, and — among such sets — has the minimum total toll. Return a boolean array ans of size n where ans[i] is true if road i belongs to at least one optimal network.
The roads marked true are exactly the critical and pseudo-critical edges of LeetCode 1489 — Find Critical and Pseudo-Critical Edges in Minimum Spanning Tree.
Example 1
m = 4
roads = [[0,1,1],[1,2,1],[2,3,2],[0,3,2],[0,2,3]]
Output: [true,true,true,true,false]
Every optimal network uses both toll-1 roads and one of the two toll-2 roads (total 4). The toll-3 road is never needed.
Example 2
m = 5
roads = [[0,1,1],[1,2,1],[2,3,2],[0,3,2],[0,4,3],[3,4,3],[1,4,6]]
Output: [true,true,true,true,true,true,false]
Constraints: 2 ≤ m ≤ 2 · 10⁴; m − 1 ≤ n ≤ 10⁵; 0 ≤ u, v < m, u ≠ v; 1 ≤ toll ≤ 10⁶; the roads connect all cities. Two roads may join the same pair of cities.
Code runner format: one JSON value per line, in order: m, roads. Implement optimalRoads(m, roads); the harness prints the boolean array.