← Uber Interview Insights

Uber·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
Jun 2026

Summary

Uber SWE interview with a maze pathfinding problem. Pretty standard BFS territory but the border-exit condition tripped me up a bit.

Questions Asked (1)

Q1

Given a 2D grid with empty cells and walls, and a starting position, find the minimum number of steps to reach the nearest empty cell on the border of the grid. The starting cell itself does not count as an exit. Return -1 if no exit is reachable.

Algorithms & Data Structures
Author's notes

My first instinct was BFS and that was right, but I fumbled the exit condition initially.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Model the grid as a graph and perform BFS from the starting cell to find the shortest path to any border cell. Treat the start as distance 0, and when exploring neighbors, check if a neighbor is on the border and empty; if so, return the current distance + 1. If BFS exhausts without finding an exit, return -1.

Pro tip: Clarify edge cases upfront: what if the start is already on the border? (It doesn't count, so you must move away and come back to a different border cell.) Also, confirm whether diagonal moves are allowed—typically only 4-directional moves are considered.

1. Clarify problem constraints and edge cases

Ask about grid dimensions, movement directions (4 or 8), and whether the start cell can be on the border. Confirm that the start cell itself is not an exit even if on the border.

2. Choose BFS for shortest path in unweighted grid

Explain that BFS is optimal because each step has equal cost. Use a queue to explore level by level, ensuring the first border cell reached is the nearest.

3. Implement BFS with visited tracking

Initialize a queue with the start cell and a visited set. For each cell, check its neighbors; if a neighbor is empty, unvisited, and on the border, return distance+1. Otherwise, enqueue it with distance+1.

4. Handle unreachable exits and return -1

If the queue empties without finding a border cell, return -1. Ensure the start cell is marked visited to avoid cycles.

5. Analyze time and space complexity

State that time complexity is O(m*n) since each cell is visited at most once, and space complexity is O(m*n) for the queue and visited set in the worst case.

Key Points to Mention

  • BFS guarantees shortest path in unweighted graphs
  • Use a queue for level-order traversal
  • Mark cells as visited to avoid infinite loops
  • Check border condition when exploring neighbors, not when dequeuing
  • Start cell does not count as an exit even if on border
  • Return -1 if BFS completes without finding an exit

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