Code RoomCache network distance
EasyPrep Room Coding #4849

Cache network distance

CodingAlgorithms & data structuresEntry–Mid~17 min

A content network runs its own backbone. The map holds site_count sites numbered 0 upward, and its fibre spans live in two parallel lists of equal length: span i joins site link_a[i] to site link_b[i], and traffic runs both ways along a span. caches lists the sites holding a copy of the library, and a site may be listed there more than once. Return one entry per site, in ascending site number order, giving how many spans separate that site from the nearest cache. A site holding a cache itself answers 0, and a site that no chain of spans connects to any cache answers -1. A span listed twice adds nothing, and a span whose two ends name the same site changes nothing. Every number given names a real site, and a map with no sites returns an empty list.

Implement
hops_to_nearest_cache(site_count: int, link_a: list[int], link_b: list[int], caches: list[int]) → list[int]
Examples
in[6,[0,1,1,3,4],[1,2,3,4,5],[0]]out[0,1,2,2,3,4]
in[5,[0,1,3],[1,2,4],[2,3]]out[2,1,0,0,1]
in[3,[],[],[]]out[-1,-1,-1]
What a strong answer looks like

State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.

0:00 of about 17 min
InputExpectedGot
[6,[0,1,1,3,4],[1,2,3,4,5],[0]][0,1,2,2,3,4]not run yetsample
[5,[0,1,3],[1,2,4],[2,3]][2,1,0,0,1]not run yetsample
[3,[],[],[]][-1,-1,-1]not run yetsample