← Uber Interview Insights

Uber·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
Jun 2026

Summary

Uber coding round for a software engineer position. One graph problem the whole time, but it had enough nuance to keep me on my toes for a while.

Questions Asked (1)

Q1

Given a directed grid or graph, find the minimum number of edge reversals needed to travel from a source node to a target node. Traversing an edge in its existing direction costs nothing; going against it costs 1 reversal.

Algorithms & Data Structures
Author's notes

I knew BFS but initially reached for Dijkstra out of habit.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Model the problem as a shortest path on a graph where each directed edge has weight 0 if traversed in its original direction and weight 1 if reversed. Use 0-1 BFS (deque) or Dijkstra's algorithm to find the minimum cost from source to target. Discuss the trade-offs between these algorithms and handle edge cases like unreachable targets.

Pro tip: Mention that 0-1 BFS is optimal for binary weights and runs in O(V+E), which is more efficient than Dijkstra's O(E log V). Also, clarify that reversing an edge is a one-time cost and the graph remains unchanged for subsequent traversals.

1. Clarify the problem and constraints

Confirm whether the graph is given as an adjacency list or grid, if edge weights are binary, and if multiple reversals of the same edge are allowed. Ask about the expected input size to choose the right algorithm.

2. Model as a weighted graph

Represent each directed edge as two directed edges: one with weight 0 (original direction) and one with weight 1 (reverse direction). This transforms the problem into finding the shortest path with non-negative weights.

3. Choose the appropriate shortest path algorithm

Since weights are 0 or 1, use 0-1 BFS with a deque for O(V+E) time. Alternatively, Dijkstra's algorithm works but is less efficient. Explain why 0-1 BFS is preferred.

4. Implement and handle edge cases

Initialize distances to infinity, set source distance to 0, and use a deque to process nodes. If the target is unreachable, return -1 or infinity. Test with small examples and edge cases like source equals target.

5. Analyze complexity and discuss optimizations

State time and space complexity: O(V+E) for 0-1 BFS. Mention that if the graph is large, early termination when the target is popped can save time. Also, note that the graph can be implicit (e.g., grid) to save space.

Key Points to Mention

  • 0-1 BFS using a deque: push front for weight 0, push back for weight 1.
  • Dijkstra's algorithm as an alternative, but with higher time complexity.
  • Graph transformation: each original edge becomes two directed edges with weights 0 and 1.
  • Time and space complexity: O(V+E) time, O(V) space for 0-1 BFS.
  • Handling unreachable target: return -1 or infinity.
  • Edge cases: source equals target, disconnected graph, multiple edges between nodes.

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