PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

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.

  • Medium
  • Nash AI
  • Coding & Algorithms
  • Software Engineer

Implement Minesweeper click and print

Company: Nash AI

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: Medium

Interview Round: Technical Screen

Implement the Minesweeper click(row, col) operation and a function to print the board. Rules: ( 1) If the clicked cell is a mine, reveal it and indicate that the game is lost. ( 2) If the clicked cell has zero adjacent mines, recursively reveal all neighboring cells. ( 3) Otherwise, reveal the number of adjacent mines for that cell. Describe the data structures you will use to track states and neighbors, the traversal strategy (BFS or DFS) and how you prevent revisiting cells, and analyze time and space complexity.

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.

You are given an `m x n` Minesweeper board represented as a 2D grid of single-character strings. Each cell is one of: - `'M'` — an unrevealed **mine** - `'E'` — an unrevealed **empty** square - `'B'` — a revealed **blank** square that has **no** adjacent mines (all 8 neighbors) - a digit `'1'`–`'8'` — a revealed square showing exactly how many of its 8 neighbors are mines - `'X'` — a revealed **mine** You are also given a `click` position `(row, col)` — the next square to reveal — that is guaranteed to be currently `'M'` or `'E'`. Apply these rules and return the resulting board: 1. If the clicked square is a **mine** (`'M'`), change it to `'X'`. The game is over. 2. If the clicked square is an **empty** square (`'E'`) with **at least one** adjacent mine, change it to the digit of how many mines are adjacent (1–8). 3. If the clicked square is an **empty** square (`'E'`) with **no** adjacent mines, change it to `'B'`, then recursively reveal **all** of its 8 neighbors using the same rules. 4. When no further squares can be revealed this way, return the board. Reveal cells using DFS (or BFS) flood fill: only `'E'` cells are ever pushed onto the frontier, and each is marked the moment it is processed, which prevents revisiting. Adjacency is the 8 surrounding cells (including diagonals). Write `solution(board, click)` that mutates and returns `board`.

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

  1. Handle the mine case first: if board[click] == 'M', set it to 'X' and return immediately.
  2. 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.
  3. 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.
  4. 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.
Last updated: Jun 26, 2026

Related Coding Questions

  • Implement Minesweeper click-to-reveal expansion - Nash AI (medium)

Loading coding console...

PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.