Fewest one-way road reversals needed to drive from a source to a target intersection
Company: Google
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: hard
Interview Round: Technical Screen
A city has `n` intersections labeled `0` to `n - 1`, connected by one-way roads. Each road is given as `[u, v]` and may be driven legally from `u` to `v`. A driver may also drive a road against its direction (from `v` to `u`), and every such traversal counts as one reversal.
Given the road list, a `source` intersection and a `target` intersection, return the minimum number of reversals needed to drive from `source` to `target`. If `source == target`, return `0`. If `target` cannot be reached from `source` even when reversals are allowed, return `-1`.
### Function Signature
```python
def min_reversals(n: int, roads: list[list[int]], source: int, target: int) -> int:
```
### Rules
- Driving road `[u, v]` from `u` to `v` costs 0 reversals. Driving it from `v` to `u` costs 1 reversal.
- Every traversal against a road's direction is counted, including repeated traversals of the same road.
- The same road may appear more than once in `roads`. Roads `[u, v]` and `[v, u]` may both be present; in that case driving from `v` to `u` is legal through the second road.
- The result is the minimum total reversal count over all routes, returned as a single integer.
### Constraints
- `1 <= n <= 10^5`
- `0 <= len(roads) <= 2 * 10^5`
- For every road `[u, v]`: `0 <= u, v < n` and `u != v`
- `0 <= source, target < n`
- The answer is at most `n - 1`, well within the 32-bit signed integer range.
### Examples
#### Example 1
```text
Input: n = 5, roads = [[0,1],[2,1],[2,3],[4,3]], source = 0, target = 4
Output: 2
```
The route 0 → 1 → 2 → 3 → 4 drives `[0,1]` legally, `[2,1]` against its direction, `[2,3]` legally and `[4,3]` against its direction. The only road touching intersection 4 leaves it, and the only way out of intersections 0 and 1 is driving `[2,1]` backward, so no route uses fewer than 2 reversals.
#### Example 2
```text
Input: n = 3, roads = [[0,1],[1,2]], source = 2, target = 2
Output: 0
```
The source is already the target.
#### Example 3
```text
Input: n = 4, roads = [[0,1],[2,3]], source = 0, target = 3
Output: -1
```
No road connects intersections 0 and 1 with intersections 2 and 3 in either direction.
Overview: A Google technical screen graph problem: one-way roads may be driven against their direction, each such traversal counting as one reversal, and you must return the fewest reversals needed to get from a source intersection to a target. It tests graph modeling, shortest paths with unequal edge costs, and handling of unreachable and trivial cases.
A city has `n` intersections labeled `0` to `n - 1`, connected by one-way roads. Each road is given as `[u, v]` and may be driven legally from `u` to `v`. A driver may also drive a road against its direction (from `v` to `u`), and every such traversal counts as one reversal.
Implement `min_reversals(n, roads, source, target)`, which returns the minimum number of reversals needed to drive from `source` to `target`. If `source == target`, return `0`. If `target` cannot be reached from `source` even when reversals are allowed, return `-1`.
### Rules
- Driving road `[u, v]` from `u` to `v` costs 0 reversals. Driving it from `v` to `u` costs 1 reversal.
- Every traversal against a road's direction is counted, including repeated traversals of the same road.
- The same road may appear more than once in `roads`. Roads `[u, v]` and `[v, u]` may both be present; in that case driving from `v` to `u` is legal through the second road and costs 0.
- The result is the minimum total reversal count over all routes, returned as a single integer.
- The answer is at most `n - 1`, so it never exceeds 2^31 - 1 and fits in `int` in Java and C++.
### Example 1
```text
Input: n = 5, roads = [[0,1],[2,1],[2,3],[4,3]], source = 0, target = 4
Output: 2
```
The route 0 → 1 → 2 → 3 → 4 drives `[0,1]` legally, `[2,1]` against its direction, `[2,3]` legally and `[4,3]` against its direction. The only road touching intersection 4 leaves it, and the only way out of intersections 0 and 1 is driving `[2,1]` backward, so no route uses fewer than 2 reversals.
### Example 2
```text
Input: n = 4, roads = [[0,1],[2,3]], source = 0, target = 3
Output: -1
```
No road connects intersections 0 and 1 with intersections 2 and 3 in either direction.
### Constraints
- `1 <= n <= 10^5`
- `0 <= len(roads) <= 2 * 10^5`
- For every road `[u, v]`: `0 <= u, v < n` and `u != v`
- `0 <= source, target < n`
- The answer is at most `n - 1`, well within the 32-bit signed integer range.
Constraints
- 1 <= n <= 10^5
- 0 <= len(roads) <= 2 * 10^5
- For every road [u, v]: 0 <= u, v < n and u != v
- 0 <= source, target < n
- The answer is at most n - 1, well within the 32-bit signed integer range.
Examples
Input: (5, [[0, 1], [2, 1], [2, 3], [4, 3]], 0, 4)
Expected Output: 2
Explanation: Example 1: the only route 0, 1, 2, 3, 4 drives [2, 1] and [4, 3] against their direction.
Input: (3, [[0, 1], [1, 2]], 2, 2)
Expected Output: 0
Explanation: Source equals target on a graph that has roads, so the answer is 0.
Hints
- Each road offers two moves between its endpoints, one along its direction and one against it. Write down what each move costs.
- Only the reversal count matters. A route that drives more roads can still need fewer reversals, as a short wrong-way road beside a longer legal loop shows.
- Decide what to return before any search when source equals target, and when no route exists even with reversals.