← Google Interview Insights

Google·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
Apr 2026

Summary

Google coding interview, one algorithmic problem about a lock and counting turns. Short session, not much context given beforehand.

Questions Asked (1)

Q1

Given a combination lock, find the minimum number of turns needed to open it.

Algorithms & Data Structures
Author's notes

Classic BFS problem once you see it, but I didn't see it right away.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Clarify the lock's mechanics and the definition of a 'turn' (e.g., rotating one dial by one position). Model the problem as a shortest path on a state graph where nodes are lock combinations and edges are single turns, then apply BFS to find the minimum number of turns from the initial to the target combination.

Pro tip: Demonstrate maturity by proactively discussing constraints and edge cases (e.g., deadends, circular dials, multiple dials) and comparing BFS with bidirectional BFS or A* for efficiency.

1. Clarify the problem

Ask questions to understand the lock's structure (number of dials, positions per dial), what constitutes a turn, and whether there are forbidden combinations. Confirm the goal is to minimize the number of turns.

2. Model as a graph

Represent each possible combination as a node. Connect two nodes with an edge if one can be reached from the other by a single turn (e.g., rotating one dial by one position).

3. Choose BFS for unweighted shortest path

Since each turn has equal cost, BFS from the initial combination will find the minimum number of turns to reach the target. Use a queue and a visited set to avoid cycles.

4. Optimize if needed

For large state spaces, consider bidirectional BFS (search from both start and target) or A* with a heuristic like the sum of minimal dial rotations. Discuss trade-offs.

5. Analyze complexity and edge cases

State time and space complexity (O(N) where N is number of reachable states). Handle edge cases: start equals target, deadends, circular dials (modulo arithmetic), and multiple dials.

Key Points to Mention

  • BFS guarantees shortest path in unweighted graphs
  • State space size: (positions per dial)^(number of dials)
  • Handling circular dials with modulo arithmetic
  • Using a visited set to avoid infinite loops
  • Bidirectional BFS can reduce search space significantly
  • A* with admissible heuristic for further optimization

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