As reported: Cohesity MTS-3, May 2025, round 1 (DSA on CoderPad with a US interviewer; working code expected). The first of two problems; a per-house BFS came first, then a multi-source BFS.
There are n houses labeled 0 to n - 1, connected by electrical wires. edges[j] = [a, b] is an undirected wire of length 1 between houses a and b; the houses form one connected graph. The houses listed in solarPanels already have solar power (cost 0). Every other house pulls electricity along the wires from its nearest solar-powered house.
Return an array dist of length n where dist[i] is the length of the shortest path from house i to any house in solarPanels (0 if house i is itself in solarPanels).
Example 1
n = 6, edges = [[0,1],[1,2],[2,3],[3,4],[4,5]], solarPanels = [0,5]
Output: [0,1,2,2,1,0]
Example 2
n = 5, edges = [[0,1],[0,2],[0,3],[3,4]], solarPanels = [4]
Output: [2,3,3,1,0]
Example 3
n = 1, edges = [], solarPanels = [0]
Output: [0]
Constraints: 1 <= n <= 10^5, 0 <= edges.length <= 2 * 10^5, 0 <= a, b < n, a != b, no duplicate wires, the graph is connected, 1 <= solarPanels.length <= n, 0 <= solarPanels[i] < n (a house may be listed more than once).
Code runner format: one JSON value per line: n, edges, solarPanels. Implement nearestSolarDistance(n, edges, solarPanels) returning the array dist.