PracHub
QuestionsLearningGuidesInterview Prep
|Home/Coding & Algorithms/Google

Minimize Direction Violations in a Directed Road Network

Last updated: Jul 24, 2026

Quick Overview

Find a route through a directed road network that minimizes travel against the original edge directions. Model forward and reverse traversal costs correctly, handle parallel edges and disconnected nodes, and choose a shortest-path approach that scales to hundreds of thousands of vertices and edges.

  • medium
  • Google
  • Coding & Algorithms
  • Software Engineer

Minimize Direction Violations in a Directed Road Network

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

# Minimize Direction Violations in a Directed Road Network There are `n` nodes numbered from `0` through `n - 1`. Every input edge `[u, v]` has an original direction from `u` to `v`, but you may physically traverse it in either direction. - Traversing an edge in its original direction costs `0`. - Traversing it against its original direction costs `1`. Given `start` and `end`, return the minimum total cost of any route from `start` to `end`. Return `-1` if the two nodes are disconnected even when edges may be traversed both ways. ## Function Signature ```python def minimum_direction_violations( n: int, edges: list[list[int]], start: int, end: int, ) -> int: ... ``` ## Constraints - `1 <= n <= 200_000` - `0 <= len(edges) <= 300_000` - `0 <= u, v < n` and `u != v` - Multiple directed edges between the same pair of nodes are allowed. - `0 <= start, end < n` ## Examples ```text Input: n = 5, edges = [[0, 1], [2, 1], [2, 3], [4, 3]], start = 0, end = 4 Output: 2 ``` ```text Input: n = 4, edges = [[0, 1], [1, 2]], start = 0, end = 3 Output: -1 ``` ```text Input: n = 3, edges = [[0, 1], [1, 2]], start = 2, end = 0 Output: 2 ```

Quick Answer: Find a route through a directed road network that minimizes travel against the original edge directions. Model forward and reverse traversal costs correctly, handle parallel edges and disconnected nodes, and choose a shortest-path approach that scales to hundreds of thousands of vertices and edges.

Related Interview Questions

  • Count Overlapping Rectangle Updates on a Grid - Google (hard)
  • Find A Threshold-Limited Path With Minimum Required Safety - Google (medium)
  • Filter Repeated Robot Status Messages - Google (medium)
  • Find the Largest Monotone Increasing Number - Google (medium)
|Home/Coding & Algorithms/Google

Minimize Direction Violations in a Directed Road Network

Google logo
Google
Jul 5, 2026, 12:00 AM
mediumSoftware EngineerOnsiteCoding & Algorithms
12
0

Minimize Direction Violations in a Directed Road Network

There are n nodes numbered from 0 through n - 1. Every input edge [u, v] has an original direction from u to v, but you may physically traverse it in either direction.

  • Traversing an edge in its original direction costs 0 .
  • Traversing it against its original direction costs 1 .

Given start and end, return the minimum total cost of any route from start to end. Return -1 if the two nodes are disconnected even when edges may be traversed both ways.

Function Signature

def minimum_direction_violations(
    n: int,
    edges: list[list[int]],
    start: int,
    end: int,
) -> int:
    ...

Constraints

  • 1 <= n <= 200_000
  • 0 <= len(edges) <= 300_000
  • 0 <= u, v < n and u != v
  • Multiple directed edges between the same pair of nodes are allowed.
  • 0 <= start, end < n

Examples

Input: n = 5, edges = [[0, 1], [2, 1], [2, 3], [4, 3]], start = 0, end = 4
Output: 2
Input: n = 4, edges = [[0, 1], [1, 2]], start = 0, end = 3
Output: -1
Input: n = 3, edges = [[0, 1], [1, 2]], start = 2, end = 0
Output: 2

Submit Your Answer to Earn 20XP

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More Google•More Software Engineer•Google Software Engineer•Google Coding & Algorithms•Software Engineer Coding & Algorithms
PracHub

Master your tech interviews with 8,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • AI Coding Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.