Quick 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.

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

  1. Each road offers two moves between its endpoints, one along its direction and one against it. Write down what each move costs.
  2. 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.
  3. Decide what to return before any search when source equals target, and when no route exists even with reversals.

Fewest Wrong-Way Drives from Source to Target

hardGoogleSoftware Engineer

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

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

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.

Examples

Example 1
Input
(5, [[0, 1], [2, 1], [2, 3], [4, 3]], 0, 4)
Output
2
Notes
Example 1: the only route 0, 1, 2, 3, 4 drives [2, 1] and [4, 3] against their direction.
Example 2
Input
(3, [[0, 1], [1, 2]], 2, 2)
Output
0
Notes
Source equals target on a graph that has roads, so the answer is 0.

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.

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Signature
def min_reversals(n: int, roads: list[list[int]], source: int, target: int) -> int:
Sample call
min_reversals(5, [[0, 1], [2, 1], [2, 3], [4, 3]], 0, 4)
Language
Case 1
Input
(5, [[0, 1], [2, 1], [2, 3], [4, 3]], 0, 4)
Expected
2
Case 2
Input
(3, [[0, 1], [1, 2]], 2, 2)
Expected
0

Browse More Questions

Show the approach

Approach

Treat each road [u, v] as two moves: u to v with cost 0 and v to u with cost 1. A duplicate road adds parallel moves with the same costs, and a road [v, u] adds its own cost-0 move from v to u, so the cheapest way across any pair of neighbouring intersections is always on offer. The answer is the cheapest route from source to target in a graph whose move costs are only 0 and 1; the number of roads driven does not matter.

The reference settles intersections in order of reversal count, the order Dijkstra's algorithm would use, with one bucket for the current count k and one for k + 1 (a 0-1 BFS). Popping an intersection that is not yet settled settles it at count k. A cost-0 move that improves a neighbour's count puts the neighbour in the current bucket, and a cost-1 move that improves it puts it in the next bucket. When the current bucket empties, the next bucket becomes current and k grows by one. A count is written only when it strictly improves and a settled intersection is skipped, so each intersection's moves are scanned once and the buckets receive at most 2 * len(roads) + 1 entries in total.

Invariant: when the bucket for count k starts, every intersection whose true minimum is below k is settled, and every intersection whose true minimum is k is either in the bucket or will be pushed into it, because its cheapest route arrives either from a count k - 1 intersection by a cost-1 move or from a count k intersection by a cost-0 move. So the first time target is popped, its count is the minimum and the search returns it at once. If no bucket holds anything before target is popped, target is unreachable even with reversals and the result is -1.

A cheapest route never repeats an intersection, so it drives at most n - 1 roads and the answer is at most n - 1. Breadth-first search by number of roads fails here: in the cycle 0 → 1 → 2 → 3 → 0 the one-road route from 0 to 3 drives [3, 0] backward for 1 reversal, while the three-road route needs none. Edge cases: source == target returns 0 before any work, a graph with no roads and source != target returns -1, and intersections outside the source's component are never reached.

Time complexity:
O(n + m), where m = len(roads)
Space complexity:
O(n + m)