Interview Questions/Coding/Alternating Channel Signal Routes

Alternating Channel Signal Routes

Preview mode. Log in to edit, run, submit, and save progress.

Medium

Alternating Channel Signal Routes

A city control room has n signal stations numbered from 0 to n - 1. Two separate one-way cable systems connect the stations: copper links and fiber links. A signal starts from station 0. For safety reasons, a route is valid only if consecutive moves use different cable systems. The first move may use either copper or fiber. A station may be reached with different last-used cable types, and those situations are considered different route states. For every station, determine the fewest links needed to reach it from station 0 while obeying the alternating-channel rule. If no valid alternating route reaches a station, mark it as -1. Input Format: You are given n, an array copperLinks, and an array fiberLinks. Each link is represented as [from, to]. Output Format: Return an array answer where answer[i] is the minimum number of valid moves needed to reach station i, or -1 if station i is unreachable.

Examples

Example 1
Input:
n = 3
copperLinks = [[0, 1], [1, 2]]
fiberLinks = []
Output:
0 1 -1

Explanation: Example shortest routes are 1: 0 -> 1 using copper. Station(s) [2] stay unreachable because every route would repeat a channel or not exist. Distances are 0 1 -1.

Approach hint

A station can be reached with a copper edge last or a fiber edge last; those are different states.

Common mistake

Skipping assumptions, edge cases, or trade-offs can make an otherwise good answer feel incomplete.

solution.cpp