Design a Minesweeper Bot That Decides Which Cell to Flag or Reveal Next
Company: Runloop
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: medium
Interview Round: Technical Screen
Design a bot that plays Minesweeper. The bot's goal is to win as often as possible, which means clearing the board without ever revealing a mine. The core of the problem: once some cells have been opened and show numbers, how does the bot decide which hidden cell to flag as a mine next, or which one to open?
A possible interface (adjust it as you clarify):
```python
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 visible board, applies the returned move, and calls it again until the game is won or lost.
### Constraints and Clarifications
- Use the standard rules. The board is a rectangular grid, and some cells hide mines; the bot never sees where they are.
- Revealing a mine loses the game. The game is won when every cell without a mine has been revealed.
- A revealed safe cell shows how many mines lie among its neighbors: the up to eight cells that touch it horizontally, vertically or diagonally.
- A flag is the player's own marker for a mine. Placing one reveals nothing.
- Board notation: `#` is a hidden cell, `F` a flagged cell, and `0` to `8` a revealed number.
### Clarifying Questions
- Does the bot know the total number of mines on the board?
- Is the first reveal guaranteed to be safe, or even guaranteed to open an area of zeros?
- When a 0 is revealed, does the game open its neighbors automatically, or does the bot have to do it?
- Are mines placed uniformly at random, so that every layout matching the visible board is equally likely?
- How large can the board be, and is there a time limit per move?
- Is success judged on a single game or as the win rate over many games?
### Part 1 — Moves that are certain
Given the visible board, find every hidden cell that is certainly a mine and every hidden cell that is certainly safe. State the rules and implement them. Then apply them to this board of 3 rows and 5 columns, where row 2 was opened by an earlier reveal of a 0:
```text
col 0 col 1 col 2 col 3 col 4
row 0 # # # # #
row 1 1 1 2 1 1
row 2 0 0 0 0 0
```
Which cells of row 0 can the bot flag, and which can it reveal, without any risk?
```hint Write the numbers as equations
Each revealed number states how many mines lie among its hidden neighbors, once the flags around it are subtracted. On this board no single number settles any cell on its own; look at numbers whose hidden neighbors overlap.
```
#### What This Part Should Cover
- How a revealed number and the flags around it become a constraint on the hidden cells
- The rules that settle cells from one number, and at least one rule that combines overlapping numbers
- Repeating the rules until nothing changes, with the correct result on the example board
### Part 2 — Choosing a cell when nothing is certain
Often no hidden cell is certainly safe, and the bot must take a risk to continue. Describe how the bot estimates, for every hidden cell, the chance that it holds a mine, and which cell it then reveals. The estimate must also cover hidden cells that touch no revealed number.
```hint Look past the nearest number
A hidden cell next to a 1 that has three hidden neighbors is not automatically a one-in-three risk. Consider which complete placements of the remaining mines agree with everything visible, and how likely each of them is.
```
#### What This Part Should Cover
- A risk estimate that treats overlapping numbers together rather than one number at a time
- How hidden cells away from the revealed area, and the total mine count, enter the estimate
- The rule for picking the cell, including ties
- The cost of an exact computation, and how to keep it tractable
### Part 3 — The complete bot and how to judge it
Put the pieces together into a bot that plays from the first reveal until the game ends. Cover the first move, the order in which the bot considers its options each turn, the running time of one turn, and how you would show that your bot wins more often than a simpler one.
```hint Win rate is a statistic
One game proves little. Decide what you would simulate, what you would compare against, and what you would record to find out why the bot loses the games it loses.
```
#### What This Part Should Cover
- The first move and the per-turn decision order
- Running time per move and how it grows with the board and the revealed boundary
- An evaluation plan: simulated games, a baseline, win rate, and an analysis of losses
### What a Strong Answer Covers
- Clarifies the rules the bot may rely on before designing: the mine total, first-reveal guarantees, automatic opening of zeros, and how success is measured
- Exhausts certain moves before taking any risk, and reasons about overlapping numbers jointly, with the right result on the example board
- A principled mine probability for every hidden cell, including cells away from the revealed area, using the total mine count when it is known
- Control of running time on large or tangled boundaries: independent regions, pruning, incremental work, fallbacks
- Testing of the deduction and probability logic, and a win-rate comparison against a baseline over many simulated games
### Follow-up Questions
- Two hidden cells are stuck as a 50/50 that no other reveal can inform. When should the bot take that guess, and can the total mine count ever settle it?
- Two candidate cells carry almost the same risk, but revealing one of them is likely to unlock a large part of the board. How do you trade risk against information?
- The board is very large and the revealed boundary forms one long connected region. How do you keep each move within a time limit?
- The total mine count is not shown. What changes in your estimates?
Overview: Design a bot that plays Minesweeper and maximizes its chance of winning. The question tests how the bot turns revealed numbers into constraints, finds cells that are certainly mines or certainly safe, estimates the risk of every hidden cell when it must guess, and keeps each move fast and measurable over many games.