← Uber Interview Insights

Uber·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
May 2026

Summary

Uber SWE interview with a tree edge reversal problem. Classic graph question but the constraints push you toward an iterative solution which is where I started second-guessing myself.

Questions Asked (1)

Q1

You're given a directed graph with n nodes and n-1 edges that forms a tree when you ignore edge directions. Given a root node, find the minimum number of edge reversals needed so every edge points away from the root.

Algorithms & Data Structures
Author's notes

Spent the first few minutes just making sure I understood the problem correctly, drew out the example on the whiteboard and traced through it manually.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Model the problem as a tree traversal from the given root, where each edge has a cost: 0 if it already points away from the root, and 1 if it points toward the root. Perform a DFS/BFS from the root, summing these costs to get the minimum number of reversals needed.

Pro tip: Clarify that the graph is a tree (n nodes, n-1 edges) and that edge directions are given; then emphasize that the optimal solution is to reverse exactly the edges that point toward the root in the rooted tree. This shows you understand the structure and avoid unnecessary complexity.

1. Understand the problem and constraints

Confirm that the graph is a tree when ignoring directions, and that we need to reorient edges so all point away from the given root. Note that reversing an edge changes its direction, and we want the minimum number of such reversals.

2. Model as a rooted tree with edge costs

Root the tree at the given root. For each edge, assign a cost: 0 if it already points away from the root (parent to child), and 1 if it points toward the root (child to parent). The total cost is the number of reversals needed.

3. Traverse the tree to compute total cost

Perform a DFS or BFS from the root, traversing edges in both directions (ignoring original direction for traversal). For each edge encountered, add its cost to a running total. This works because the tree has no cycles, so each edge is visited once.

4. Return the total cost

After traversing all edges, the accumulated sum is the minimum number of reversals. Return this value.

Key Points to Mention

  • The graph is a tree (n nodes, n-1 edges) when ignoring directions, so there is a unique path between any two nodes.
  • Rooting the tree at the given root defines a parent-child relationship for each edge.
  • An edge needs reversal if and only if it points from child to parent in the rooted tree.
  • DFS or BFS can traverse the tree in O(n) time, visiting each edge once.
  • The total number of reversals is simply the count of edges that point toward the root.
  • Space complexity is O(n) for the adjacency list and recursion stack (or queue).

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.