← Microsoft Interview Insights

Microsoft·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
May 2026

Summary

Microsoft coding interview, one problem the whole session. The 15-puzzle minimum moves question sounds like a classic BFS warmup until you realize the state space is enormous and you need to actually think about it.

Questions Asked (1)

Q1

Given a 4x4 sliding puzzle board with tiles numbered 1 through 15 and one blank space, find the minimum number of moves to reach the solved configuration using BFS. Return -1 if the configuration is unsolvable.

Algorithms & Data Structures
Author's notes

I jumped straight to BFS which was right, but encoding the board state tripped me up for a few minutes.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Model the puzzle as a state-space search problem where each state is a permutation of the 16 positions. Use BFS to explore moves level by level, tracking visited states to avoid cycles, and return the depth when the goal state is reached. Before searching, check solvability using the inversion parity rule to quickly return -1 for unsolvable configurations.

Pro tip: Mention that BFS is optimal for unweighted moves but can be memory-intensive; for a 4x4 puzzle, bidirectional BFS or A* with Manhattan distance heuristic would be more efficient in practice. Also, precompute solvability to avoid unnecessary search.

1. Represent the state

Encode the board as a string or tuple of 16 characters (e.g., '123456789ABCDEF0' with '0' for blank) to enable hashing and fast comparison.

2. Check solvability

Count inversions (ignoring blank) and use the parity rule: for a 4x4 board, the puzzle is solvable if the number of inversions plus the row of the blank from the bottom is even.

3. Initialize BFS

Use a queue for BFS, starting with the initial state at depth 0. Maintain a visited set to avoid revisiting states.

4. Explore neighbors

For each state, generate all valid moves by swapping the blank with adjacent tiles. If a neighbor is the goal, return depth+1; else if unvisited, add to queue and visited set.

5. Handle unsolvable

If the queue empties without reaching the goal, return -1. (Alternatively, return -1 immediately after solvability check fails.)

Key Points to Mention

  • State representation and hashing for efficient visited set
  • BFS guarantees shortest path in unweighted graphs
  • Solvability condition using inversion parity
  • Time and space complexity: O(b^d) where b is branching factor (up to 4) and d is depth
  • Optimization: bidirectional BFS or A* with Manhattan distance heuristic
  • Handling of the blank space and move generation

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