Reported September 2026
Googlegraph

Minimum Direction Violations

Reported by candidates from Google's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Google OA. Under 2s to a working solution.
Founder's read

Google's September 2026 OA reports include Minimum Direction Violations, and it looks like a graph problem until you see what it really reduces to: a shortest path where every edge costs 0 or 1. That's 0-1 BFS, not Dijkstra, and not a reachability check. You've got a directed graph, you can walk any edge backward, and backward steps cost one. If you know the deque trick, it's maybe fifteen lines. If you blank on it, StealthCoder can run invisibly during the live OA and hand you the solution as a safety net.

The problem

You are given a directed graph with n vertices numbered from 0 to n - 1. Each pair [u, v] in edges is an original directed edge from u to v.
You may traverse every listed edge in either direction:
Traversing from u to v, the original direction, costs 0 violations.
Traversing from v to u, against the original direction, costs 1 violation.
Return the minimum total number of direction violations needed to travel from start to end.
Interview Follow-Ups
Negative edge weights: If a generalized version allows negative edge weights, 0-1 BFS no longer applies. Use Bellman-Ford, and handle any reachable negative-cycle policy required by that version.
Multiple sources and destinations: Add a super-source with zero-cost edges to every source and a super-sink with zero-cost edges from every destination. Then solve one shortest-path query from the super-source to the super-sink. With the original 0/1 costs, 0-1 BFS remains valid.
These follow-ups are explanatory and do not change the judged function.

Function
minimumDirectionViolations(n: int, edges: int[][], start: int, end: int) → int

Examples
Example 1
n = 5
edges = [[0,1],[2,1],[2,3],[4,3]]
start = 0
end = 4
return = 2
Follow 0 -> 1, reverse 2 -> 1 to move 1 -> 2, follow 2 -> 3, and reverse 4 -> 3 to move 3 -> 4. Exactly two traversals oppose their original directions, and no path uses fewer violations.
Example 2
n = 4
edges = [[0,1],[1,2],[2,3]]
start = 0
end = 3
return = 0
The path 0 -> 1 -> 2 -> 3 follows every original edge direction, so its total violation cost is 0.
Example 3
n = 3
edges = [[1,0],[0,2],[2,1]]
start = 0
end = 1
return = 0
Reversing the direct edge 1 -> 0 would cost 1, but the longer path 0 -> 2 -> 1 follows original directions and costs 0.

Constraints
1 <= n <= 100000
0 <= edges.length <= 200000
Every entry in edges contains exactly two valid vertex indices.
0 <= start, end < n
end is reachable from start when all edge directions are ignored.

Reported by candidates. Source: FastPrep

Pattern and pitfall

Build an adjacency list where each edge [u, v] adds (v, 0) to u and (u, 1) to v. Run 0-1 BFS from start with a deque and a dist array filled with infinity. Pop from the front. For a 0-cost edge, push the neighbor to the front if you improved its distance. For a 1-cost edge, push it to the back. Return dist[end]. The common pitfall is plain BFS, which counts edges instead of violations and fails Example 3. Another is using a visited set that locks nodes too early. Only finalize on improvement of dist. With n up to 100000 and 200000 edges, this runs in O(n + m). Dijkstra works too, but it's slower than you need. If the deque logic slips under pressure, StealthCoder is the hedge during the live OA.

StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.

If this hits your live OA

You can drill Minimum Direction Violations cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. If you're reading this with an OA window open, you're who this was built for.

Get StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as minimum edge reversals so every node is reachable. If you have time before the OA, drill that.

⏵ The honest play

You've seen the question. Make sure you actually pass Google's OA.

Google reuses patterns across OAs. If you're reading this with an OA window open, you're who this was built for. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Minimum Direction Violations FAQ

What's the trick in Minimum Direction Violations?+

Model each original edge as two weighted edges: forward with cost 0 and reverse with cost 1. Then it's a shortest path with only 0 and 1 weights, so 0-1 BFS with a deque solves it in linear time. No heap needed.

Can I just use Dijkstra?+

Yes. It's correct and passes the constraints with O((n + m) log n). 0-1 BFS is cleaner and faster, and it's what the follow-up text points to. If you forget the deque version, write Dijkstra and move on.

Why does plain BFS fail here?+

Plain BFS minimizes the number of edges, not violations. Example 3 shows it: the direct reversed edge is one hop but costs 1, while the two-hop path costs 0. Weights matter, so you need 0-1 BFS or Dijkstra.

How do I handle the follow-ups about negative weights or multiple sources?+

For negative weights, 0-1 BFS breaks and you'd use Bellman-Ford with a negative-cycle policy. For multiple sources and destinations, add a super-source and super-sink with zero-cost edges and run one query. They don't change the judged function.

How do I prep for this in 48 hours?+

Write 0-1 BFS from scratch twice. Practice building the doubled adjacency list and updating dist only on strict improvement. Test on the three examples, plus a graph with no edges where start equals end. Edge case: start equals end returns 0.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Google.

OA at Google?
Invisible during screen share
Get it