PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep
|Home/Coding & Algorithms/Uber

Choose root to minimize edge reversals

Last updated: Mar 29, 2026

Quick Overview

This question evaluates understanding of directed versus undirected graph representations, tree rooting, and optimization of edge orientations, testing competency in graph algorithms and tree re-rooting reasoning.

  • medium
  • Uber
  • Coding & Algorithms
  • Software Engineer

Choose root to minimize edge reversals

Company: Uber

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

## Problem You are given a connected graph with `n` nodes labeled `0..n-1` and `n-1` directed edges. If you ignore directions, the edges form a **tree** (i.e., the underlying undirected graph is a tree). You may pick any node `r` as the **root**. You are allowed to **reverse** the direction of any edge; reversing one edge costs `1`. Your goal is to choose a root `r` and reverse as few edges as possible so that, in the final directed tree, **every edge points away from the root** (equivalently: for every node `v != r`, the unique path from `r` to `v` follows edge directions from parent to child). ### Input - Integer `n` - List of directed edges `edges`, where each edge is a pair `(u, v)` meaning a directed edge `u -> v` ### Output Return the **minimum number of edge reversals** needed over all choices of root `r`. ### Constraints (typical) - `1 <= n <= 2 * 10^5` - `len(edges) = n - 1` - The underlying undirected graph is connected (a tree) ### Example If `n = 3` and `edges = [(0,1), (2,0)]`: - Choosing root `0` requires reversing `(2,0)` to `(0,2)` → cost `1` - Choosing root `2` requires reversing `(0,1)` and `(2,0)`? (check orientation away from 2) → higher So the answer is `1`.

Quick Answer: This question evaluates understanding of directed versus undirected graph representations, tree rooting, and optimization of edge orientations, testing competency in graph algorithms and tree re-rooting reasoning.

Related Interview Questions

  • Maximize Throughput and Count Trigger Components - Uber (medium)
  • Replace Dashes With Nearest Letters - Uber (medium)
  • Find Earliest Column With One - Uber (easy)
  • Solve Wonderful Strings and Grid Queries - Uber (hard)
  • Count Islands After Land Additions - Uber (medium)
Uber logo
Uber
Feb 11, 2026, 12:00 AM
Software Engineer
Technical Screen
Coding & Algorithms
5
0
Loading...

Problem

You are given a connected graph with n nodes labeled 0..n-1 and n-1 directed edges. If you ignore directions, the edges form a tree (i.e., the underlying undirected graph is a tree).

You may pick any node r as the root. You are allowed to reverse the direction of any edge; reversing one edge costs 1.

Your goal is to choose a root r and reverse as few edges as possible so that, in the final directed tree, every edge points away from the root (equivalently: for every node v != r, the unique path from r to v follows edge directions from parent to child).

Input

  • Integer n
  • List of directed edges edges , where each edge is a pair (u, v) meaning a directed edge u -> v

Output

Return the minimum number of edge reversals needed over all choices of root r.

Constraints (typical)

  • 1 <= n <= 2 * 10^5
  • len(edges) = n - 1
  • The underlying undirected graph is connected (a tree)

Example

If n = 3 and edges = [(0,1), (2,0)]:

  • Choosing root 0 requires reversing (2,0) to (0,2) → cost 1
  • Choosing root 2 requires reversing (0,1) and (2,0) ? (check orientation away from 2) → higher So the answer is 1 .

Submit Your Answer

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More Uber•More Software Engineer•Uber Software Engineer•Uber 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
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL 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.