← Uber Interview Insights

Uber·Software Engineer·Technical Phone Screen·Intermediate

IntermediatePrefer not to say
May 2026

Summary

Uber SWE coding round, got a graph shortest path problem with a twist on stop constraints. Pretty standard fare for a tech interview but the edge cases kept me second-guessing myself the whole way through.

Questions Asked (1)

Q1

Given a list of directed flights with prices between n cities, find the cheapest route from a source city to a destination city using at most k stops. Return -1 if no valid route exists.

Algorithms & Data Structures
Author's notes

My first instinct was plain Dijkstra but then the stop constraint throws a wrench in it because you can't just track visited nodes the usual way.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Model the problem as a shortest path with a constraint on the number of edges (stops). Use BFS with a priority queue (Dijkstra) where the state includes the city and the number of stops used so far, or use Bellman-Ford with exactly k+1 iterations. Compare BFS and Bellman-Ford approaches, discussing time and space complexity.

Pro tip: Clarify whether the graph can have cycles and whether prices are positive; this affects algorithm choice. Mention that BFS with a queue works if all edge weights are equal, but since prices vary, Dijkstra or Bellman-Ford is needed.

1. Clarify the problem

Confirm details: directed graph, non-negative prices, at most k stops (i.e., at most k+1 edges), return -1 if no route. Ask about constraints (n, k) to choose the right algorithm.

2. Choose an algorithm

Consider Bellman-Ford with k+1 iterations (O(k * E)) or Dijkstra with state (city, stops) (O(E log V) but with extra dimension). Discuss trade-offs.

3. Implement the solution

For Bellman-Ford: initialize distances to infinity, set dist[src]=0, relax all edges k+1 times, but use a copy of distances to avoid using more than k stops. For Dijkstra: use a priority queue storing (cost, city, stops), and track best cost per (city, stops).

4. Handle edge cases

Check if source equals destination (0 stops, cost 0), if k=0 (only direct flights), and if no path exists (return -1). Also handle disconnected graphs.

5. Analyze complexity and test

State time and space complexity. Walk through a small example to verify correctness, and discuss potential optimizations (e.g., early termination).

Key Points to Mention

  • Graph representation: adjacency list or edge list
  • Bellman-Ford with exactly k+1 iterations to enforce stop limit
  • Dijkstra with state (city, stops) and priority queue
  • Time complexity: O(k * E) for Bellman-Ford, O(E log V) for Dijkstra with state
  • Space complexity: O(V) for Bellman-Ford, O(V * k) for Dijkstra
  • Edge cases: source == destination, k=0, no path, negative cycles (not applicable if prices non-negative)

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