Quick Overview

This question evaluates a candidate's competence in data structures, graph traversal and flood-fill concepts, complexity analysis, and scalable system design for sparse board representations.

Design Minesweeper and Optimize Click Performance

Company: Bridge

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

Design a Minesweeper game. At game start, initialize an m×n board by randomly placing k bombs. Implement: ( 1) printBoard(): return a human-readable string of the current player-visible board (unrevealed cells remain hidden; revealed cells show 0–8; you may optionally support flags but keep the API clear). ( 2) click(r, c): if the cell is a bomb, mark game over; otherwise reveal the cell; if its adjacent-bomb count is zero, reveal the contiguous region per standard Minesweeper rules; handle repeated clicks, boundaries, and idempotency. Describe your data structures, key algorithms (e.g., BFS/DFS flood fill), and time/space complexity. Provide test cases. Follow-up: For very large, sparse boards (huge m, n with small k), propose and analyze optimizations to make click() fast and memory-efficient (e.g., lazy board generation, sparse data structures, on-demand neighbor counts, caching, or pruning), and discuss trade-offs.

Quick Answer: This question evaluates a candidate's competence in data structures, graph traversal and flood-fill concepts, complexity analysis, and scalable system design for sparse board representations.

Part 1: Simulate a Minesweeper Board

Implement the core Minesweeper behavior in a single function. For deterministic grading, the bomb locations are given directly instead of being randomized. The function must simulate two operations: `click r c` and `print`. A click on a bomb ends the game and shows that bomb as `X`. A click on a safe numbered cell reveals only that cell. A click on a safe zero cell reveals its full zero-region using standard Minesweeper flood fill, plus all bordering numbered cells. Repeated clicks must be idempotent, out-of-bounds clicks must be ignored, and after game over, future clicks do nothing. The `print` operation should return the current player-visible board as a string with rows separated by newline characters and cells separated by single spaces. Hidden cells are `#`.

Constraints

  • 1 <= m, n <= 200
  • 0 <= len(bombs) <= m * n
  • Bomb coordinates are distinct and within bounds
  • 0 <= len(operations) <= 10^4
  • Click coordinates may be out of bounds; such clicks are ignored
  • Coordinates are 0-indexed

Examples

Input: (3, 3, [[0, 0]], ["click 2 2", "print"])

Expected Output: ["# 1 0\n1 1 0\n0 0 0"]

Explanation: Clicking a zero at the bottom-right reveals the entire connected zero region and its bordering numbered cells.

Input: (2, 2, [[1, 1]], ["click 1 1", "print", "click 0 0", "print"])

Expected Output: ["# #\n# X", "# #\n# X"]

Explanation: The first click hits a bomb and ends the game. The second click is ignored, so both printed boards are identical.

Hints

  1. Precompute the adjacent bomb count for every non-bomb cell once at the start.
  2. For revealing a zero cell, use BFS or DFS over 8 directions, and reveal bordering numbered cells as you expand.

Part 2: Sparse Minesweeper Click Queries on a Huge Board

You are given a very large Minesweeper board with sparse bombs. Building the full `m x n` board is impossible. Each click query starts from a completely fresh hidden board and asks: how many cells would be revealed by standard Minesweeper rules if the player clicked cell `(r, c)`? Return `-1` if the click hits a bomb. Return `1` if it hits a numbered safe cell. If it hits a zero cell, reveal its full zero-region using 8-direction connectivity, plus all bordering numbered cells, and return the total number of revealed cells. The goal is to answer many queries quickly while using memory proportional to the number of bombs, not the board area.

Constraints

  • 1 <= m, n <= 10^9
  • 0 <= len(bombs) <= 200
  • 1 <= len(clicks) <= 10^5
  • Bomb coordinates are distinct and within bounds
  • Click coordinates are within bounds
  • Coordinates are 0-indexed

Examples

Input: (1000000000, 1000000000, [], [[123456789, 987654321]])

Expected Output: [1000000000000000000]

Explanation: With no bombs, every cell has count 0, so one click reveals the entire board.

Input: (5, 5, [[2, 2]], [[0, 0], [1, 1], [2, 2]])

Expected Output: [24, 1, -1]

Explanation: Corner click reveals all 24 safe cells, the adjacent numbered cell reveals only itself, and clicking the bomb returns -1.

Hints

  1. Only rows and columns within distance 1 of some bomb can contain non-zero cells. Everything else is guaranteed to be zero.
  2. Coordinate-compress the interesting rows and columns, build zero-components on the compressed grid, and precompute each component's reveal size.

Loading coding console...