Design Minesweeper
💣 What is Minesweeper? Minesweeper is a puzzle game on a grid with mines hidden under some cells. You click a cell to reveal it. A safe cell shows how many of its eight neighbors are mines, and a cell with no mines around it opens up the area around it on its own. Reveal a mine and you lose; reveal every safe cell and you win. Players can also plant flags on cells they think are mines.
In an interview, Minesweeper tests two things at once. The first is plain modeling: what a cell knows, what the board knows, and what the game knows. The second is one real algorithm, the flood reveal, which is a breadth-first search on a grid. Most candidates know the game, so the interviewer will expect exact rules, a flood that never counts a cell twice, and a clean answer for where the mines come from.
Requirements
"Design Minesweeper. The player can reveal cells and place flags. Tell me when they win or lose."
Clarifying Questions
You: How are the mines placed? Randomly?
Interviewer: In the real game, yes. For this design, the caller gives you the board size and the list of mine positions. We may talk about random placement later.
You: What happens when the player reveals a cell with no mines around it?
Interviewer: It opens all its neighbors, and any neighbor that also has no mines around it keeps spreading. The spreading stops at cells that show a number.
You: What do flags do?
Interviewer: A flag marks a hidden cell. A flagged cell cannot be revealed, not even by the spreading. The player can remove the flag again.
You: When does the player win? Do they have to flag every mine?
Interviewer: No. They win as soon as every safe cell is revealed. Flags are just notes for the player.
You: What should a reveal return? And what does the player see?
Interviewer: Return how many cells the reveal opened, or -1 if it hit a mine. Also give me a way to print the board as text, one string per row. After a loss, show where all the mines were.
You: What about bad input, like a cell outside the board, or a move after the game ended?
Interviewer: Ignore it: return 0 for a reveal and false for a flag, and change nothing.
The first answer shapes the design more than it seems. Because mines come from the caller, the game itself contains no randomness, and that is what makes it testable. We will keep it that way even when we add random boards.
💡 Interview tip: Ask whether flags count toward winning. Many people assume "flag every mine" is the win condition. Most versions of the game use "reveal every safe cell", and the two lead to different counters in your code.
Final Requirements
- The board has
rows × colscells. The caller gives the mine positions when the game starts. Every cell starts hidden. - Each safe cell has a number: how many of its 8 neighbors are mines.
- Revealing a hidden, unflagged safe cell shows it. If its number is 0, its hidden neighbors are revealed too, and the rule repeats from every newly revealed 0. Flagged cells are never revealed this way.
- Revealing a mine loses the game. All mines are then shown.
- The game is won as soon as every safe cell is revealed.
- A hidden cell can be flagged and unflagged. A flagged cell cannot be revealed.
- Moves outside the board, on revealed cells, or after the game ended change nothing.
- The board can be printed as text, and the game reports its status: playing, won or lost.
Out of scope
- Random mine placement and a safe first click (we come back to both in Extensibility).
- A timer, high scores and difficulty presets.
- "Chording" (clicking a number to open its neighbors) and question-mark flags.
- Drawing the board in a real UI.