Fewest One-Way Road Reversals to Reach the Sort Center
Last updated: November 29, 2026
Quick Overview
A city's roads are one-way. A truck at depot 0 must reach the sort center at node n - 1, and the city can flip the direction of any road at a cost of one permit each. Return the minimum number of reversals needed, or -1 if impossible.
Amazon
November 29, 20260
0
0 solved
A city's roads are one-way. A truck at depot 0 must reach the sort center at node n - 1, and the city can flip the direction of any road at a cost of one permit each. Return the minimum number of reversals needed, or -1 if impossible.
This shows up in Amazon logistics-flavored screens to see whether you can turn a constraint into edge weights. The best answers model forward edges with weight 0 and reversed edges with weight 1 and use 0-1 BFS with a deque.
What the Interviewer Expects
- Add each road forward with cost 0 and backward with cost 1
- Recognize 0-1 weights and use a deque instead of a heap
- Push zero-cost moves to the front and one-cost moves to the back
- Return the distance to the target
- Compare to Dijkstra's O(E log V)
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- How would you return which roads to reverse?
- What if reversing different roads costs different amounts?
- How would you compute the minimum reversals for every node to reach the sort center at once?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Input: n intersections and a list of directed roads (u, v). Output: the minimum number of roads to reverse so that a directed path exists from 0 to n - 1, or -1 if the underlying undirected graph ...
High-Level Design
For every road u -> v add edge (u, v, 0) and edge (v, u, 1). A path in this doubled graph corresponds to a route that uses some roads forward and reverses the rest, and its weight counts reversals. Wi...