KatInterview

Minesweeper Solver: Deduction Rules and Mine Probabilities

Data Structure DesignModel answer
MediumModel answerData Structure Designminesweeperconstraint-satisfactionprobability

Last updated

Design a Minesweeper bot that derives certain mines and safe cells from overlapping numbers, estimates mine probabilities when it must guess, and is evaluated by win rate.

You are building an automated Minesweeper player. Its objective is to win as often as possible: open every safe cell without ever opening a mine. The bot cannot see the mines, only the visible board.

Suggested interface (refine it as you clarify requirements):

Move = tuple[str, int, int]   # ("reveal" or "flag", row, col)

class MinesweeperBot:
    def __init__(self, rows: int, cols: int, total_mines: int | None): ...
    def next_move(self, board: list[list[str]]) -> Move: ...

The game calls next_move with the current board, applies the move, and repeats until the game is won or lost.

Rules and notation

  • The board is a rectangular grid. Each hidden cell may contain a mine.
  • Opening a mine loses. Opening every non-mine cell wins.
  • An opened safe cell shows the number of mines among its up to eight neighbors (horizontal, vertical, diagonal).
  • A flag is your own marker for a suspected mine and reveals nothing.
  • # = hidden, F = flagged, 0-8 = opened number.

State any assumptions you need, such as whether the bot knows the total mine count, whether the first click is safe, whether zeros open their neighbors automatically, how mines are distributed, and the board size and time budget per move.

Part 1: Certain moves. Describe and implement rules that find every hidden cell that is certainly a mine or certainly safe. Apply them to this 3x5 board, where row 2 came from opening a zero:

       c0 c1 c2 c3 c4
row 0   #  #  #  #  #
row 1   1  1  2  1  1
row 2   0  0  0  0  0

Which cells of row 0 can be flagged and which can be opened with no risk?

Part 2: Guessing. When nothing is certain, explain how you estimate the mine probability of every hidden cell (including cells not adjacent to any number) and which cell you open. Cover how overlapping numbers are handled together, how the total mine count and distant cells enter the estimate, how ties are broken, and how you keep the computation tractable.

Part 3: The whole bot. Describe the first move, the order of decisions each turn, the running time of one turn, and how you would demonstrate that your bot beats a simpler one.

Hints

Hint 1

Turn every opened number into an equation: the set of hidden neighbors must contain exactly (number minus adjacent flags) mines. Think of the hidden cells as 0/1 variables.

Hint 2

Besides the single-number rules (all hidden neighbors are mines, or none are), compare two constraints where one hidden set is a subset of the other: the difference of their counts tells you about the leftover cells.

Hint 3

For probabilities, split the frontier into independent components, enumerate valid assignments by backtracking, and weight each assignment by the number of ways to place the remaining mines among the non-frontier cells.