← Uber Interview Insights

Uber·Machine Learning Engineer·Technical Phone Screen·Senior

SeniorPrefer not to say
May 2026

Summary

Uber ML Engineer interview with a graph traversal coding problem. Pretty standard algorithmic round, nothing too wild, but the follow-up about weighted edges was a nice curveball.

Questions Asked (1)

Q1

Given n cities connected by bidirectional edges and a starting city, return all cities sorted first by shortest distance from the source, then by city index for ties.

Algorithms & Data Structures
Author's notes

Went with BFS pretty quickly since the edges seemed unweighted at first glance.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Model the cities and edges as an unweighted graph and run BFS from the source to compute shortest distances to all reachable cities. Then sort the cities by distance ascending and, for ties, by city index ascending, returning the sorted list.

Pro tip: Clarify edge cases upfront: disconnected cities should be excluded, and if the graph is large, mention that BFS is O(V+E) while sorting adds O(V log V). Also, note that if the graph were weighted, Dijkstra's algorithm would be needed instead.

1. Clarify the problem

Confirm that edges are unweighted, the graph may be disconnected, and the output should include only reachable cities. Ask about input format and constraints.

2. Choose BFS for shortest paths

Explain that BFS is optimal for unweighted graphs, giving shortest distances in O(V+E) time. Mention that Dijkstra's would be overkill here.

3. Implement BFS

Use a queue to traverse from the source, track distances in an array or hash map, and mark visited cities to avoid cycles.

4. Sort results

Collect all reachable cities with their distances, then sort by distance ascending and city index ascending for ties. Return the sorted list.

5. Analyze complexity and edge cases

State time complexity O(V+E + V log V) and space O(V). Discuss disconnected components, source not in graph, and large inputs.

Key Points to Mention

  • BFS guarantees shortest path in unweighted graphs
  • Time complexity: O(V+E) for BFS plus O(V log V) for sorting
  • Space complexity: O(V) for queue, visited set, and distance map
  • Handling disconnected cities by excluding them from output
  • Tie-breaking by city index requires a custom comparator
  • Alternative: Dijkstra's algorithm if edges had weights

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