← Uber Interview Insights

Uber·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
Jun 2026

Summary

Uber SWE interview with a classic BFS lock problem. Nothing too crazy but it required knowing your graph traversal cold, no hand-holding.

Questions Asked (1)

Q1

You have a 4-wheel combination lock where each wheel has digits 0-9 and wraps around. Starting from '0000', given a list of forbidden codes and a target code, find the minimum number of single-wheel turns to reach the target, or return -1 if it's not possible.

Algorithms & Data Structures
Author's notes

Took me a minute to see it as a graph problem.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Model the lock states as nodes in a graph where each state is a 4-digit string, and edges connect states that differ by one digit (with wrap-around). Use BFS from '0000' to find the shortest path to the target, skipping forbidden states. If the target is reached, return the distance; else return -1.

Pro tip: Clarify edge cases upfront: if the target is forbidden or the start is forbidden, return -1 immediately. Also, mention that BFS is optimal for unweighted graphs, and consider bidirectional BFS for large state spaces to reduce time.

1. Understand the problem and constraints

Confirm that each move changes one digit by ±1 with wrap-around (0↔9), and that forbidden codes cannot be visited. Clarify that the target may be unreachable.

2. Model as a graph

Represent each 4-digit code as a node. Edges connect codes that differ by one digit in one position (with wrap-around). This forms a graph with up to 10,000 nodes.

3. Choose BFS for shortest path

Since each move has equal cost, BFS from '0000' guarantees the minimum number of turns. Use a queue and a visited set to avoid cycles and forbidden states.

4. Implement BFS with neighbor generation

For each popped state, generate up to 8 neighbors by incrementing/decrementing each digit (mod 10). Skip neighbors that are forbidden or already visited. If target is found, return depth.

5. Handle edge cases and return result

If BFS exhausts without reaching target, return -1. Also check if start or target is forbidden at the beginning and return -1 if so.

Key Points to Mention

  • BFS guarantees shortest path in unweighted graphs
  • State space size is 10^4 = 10,000, which is small enough for BFS
  • Neighbor generation: for each of 4 positions, try +1 and -1 modulo 10
  • Use a visited set to avoid revisiting states and to skip forbidden codes
  • Edge cases: start or target forbidden, target unreachable
  • Time complexity O(10^4 * 8) = O(1) effectively, space O(10^4)

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