Implement Minesweeper click and print
Company: Nash AI
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: Medium
Interview Round: Technical Screen
Quick Answer: Implement Minesweeper click and print evaluates algorithm design, data structures, correctness, complexity, edge cases, and implementation details in a realistic interview setting. A strong answer states assumptions, handles edge cases, explains trade-offs, and shows how to validate the result clearly.
Constraints
- 1 <= m, n <= 50 (board has m rows and n columns)
- Each board cell is one of 'M', 'E', 'B', 'X', or a digit '1'–'8'.
- click = (row, col) with 0 <= row < m and 0 <= col < n.
- The clicked cell is guaranteed to be 'M' or 'E'.
- Adjacency is the 8 surrounding cells (horizontal, vertical, and diagonal).
Examples
Input: ([['E','E','E','E','E'],['E','E','M','E','E'],['E','E','E','E','E'],['E','E','E','E','E']], (3, 0))
Expected Output: [['B','1','E','1','B'],['B','1','M','1','B'],['B','1','1','1','B'],['B','B','B','B','B']]
Explanation: Clicking the bottom-left empty cell, which has 0 adjacent mines, triggers a flood fill. The reveal spreads across all blank ('B') cells and stops at the ring of '1' cells bordering the single mine; the cells directly above and below the mine remain 'E' because they were never reached by a zero-adjacent expansion.
Input: ([['B','1','E','1','B'],['B','1','M','1','B'],['B','1','1','1','B'],['B','B','B','B','B']], (1, 2))
Expected Output: [['B','1','E','1','B'],['B','1','X','1','B'],['B','1','1','1','B'],['B','B','B','B','B']]
Explanation: Continuing the previous board, the player clicks directly on the mine. The single mine cell flips from 'M' to 'X' and the game is lost; nothing else changes.
Input: ([['M']], (0, 0))
Expected Output: [['X']]
Explanation: A 1x1 board whose only cell is a mine: clicking it reveals the mine ('M' -> 'X').
Input: ([['E']], (0, 0))
Expected Output: [['B']]
Explanation: A 1x1 empty board: the cell has 0 adjacent mines, so it becomes a blank 'B'.
Input: ([['E','M'],['E','E']], (1, 0))
Expected Output: [['E','M'],['1','E']]
Explanation: The clicked cell (1,0) is empty but has one diagonally adjacent mine at (0,1), so it shows '1' and does NOT flood-fill. The other empty cells stay 'E'.
Input: ([['E','E','E'],['E','M','E'],['E','E','E']], (0, 0))
Expected Output: [['1','E','E'],['E','M','E'],['E','E','E']]
Explanation: Clicking the corner (0,0), which is diagonally adjacent to the center mine, reveals only that one cell as '1'. Because the count is non-zero there is no recursion, so every other empty cell remains 'E'.
Hints
- Handle the mine case first: if board[click] == 'M', set it to 'X' and return immediately.
- Use a DFS stack (or BFS queue) seeded with the clicked cell. Only ever push cells that are currently 'E'; mark each cell the instant you pop it so it is never revisited.
- For each popped empty cell, count mines among its 8 neighbors. If the count is > 0, write that digit and STOP — do not expand its neighbors. Only when the count is 0 do you write 'B' and push the neighboring 'E' cells.
- Because you only change cells from 'E' to a final state, the board itself acts as your visited set — no separate visited matrix is needed.