Minesweeper

MediumMatrixdfs

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

You are given a Minesweeper board as a grid of characters and the cell the player clicks. Cell meanings: - 'M' an unrevealed mine, 'E' an unrevealed empty cell - 'B' a revealed blank (no adjacent mines), '1'..'8' a revealed cell showing how many mines touch it (8 directions) - 'X' a revealed mine The clicked cell click = [row, col] is always unrevealed ('M' or 'E'). Apply these rules and return the updated board: 1. Clicking a mine turns it into 'X', and nothing else changes. 2. Clicking an 'E' with at least one adjacent mine turns it into the digit for that count, and nothing else changes. 3. Clicking an 'E' with no adjacent mines turns it into 'B', and every adjacent unrevealed 'E' is revealed by the same rules, recursively. The cascade stops at cells that show digits.

Examples

Input: board = [["E","E","E","E","E"],["E","E","M","E","E"],["E","E","E","E","E"],["E","E","E","E","E"]], click = [3,0]
Output: [["B","1","E","1","B"],["B","1","M","1","B"],["B","1","1","1","B"],["B","B","B","B","B"]]
(3,0) has no neighboring mine, so the reveal floods outward until it hits cells bordering the mine. (0,2) stays hidden because only digits surround it.
Input: board = [["B","1","E","1","B"],["B","1","M","1","B"],["B","1","1","1","B"],["B","B","B","B","B"]], click = [1,2]
Output: [["B","1","E","1","B"],["B","1","X","1","B"],["B","1","1","1","B"],["B","B","B","B","B"]]
The click lands on the mine, which becomes `X`.

Constraints

  • `1 <= m, n <= 50`
  • `board[i][j]` is one of `'M'`, `'E'`, `'B'`, `'X'`, `'1'`-`'8'`
  • `board[click[0]][click[1]]` is `'M'` or `'E'`

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More dfs problems