KatInterview

Deterministic Mine Grid Plus Letter-Path Check

Trees & GraphsAuto-graded · 18 test cases
MediumAuto-gradedTrees & Graphsgridbacktrackingdfs

Last updated

Build a mine-count grid with mines in row-major order, then use DFS backtracking to check whether a word can be traced through a separate letter grid.

Write one function that does two independent jobs on two different grids and returns both results.

Job 1: Mine-count grid

You are given rows, cols and mine_count. Build a rows x cols grid of single-character strings as follows:

  1. Place mines in the first mine_count cells in row-major order. That means row 0 from left to right, then row 1 from left to right, and so on. A mine cell holds the string "M".
  2. Every other cell holds the number of mines among its up-to-8 neighbours (horizontal, vertical and diagonal), written as a single-digit string from "0" to "8".

Mine placement is fixed rather than random, so the output is fully determined by the inputs.

Job 2: Word tracing

You are also given grid, a 2D list of strings, and a string target. Decide whether target can be spelled by walking through grid:

  • The first character is read from any cell. Each following character is read from a cell directly above, below, left or right of the previous one.
  • A cell may be used at most once within one walk.
  • A cell matches a character only if its string equals that single character exactly. A cell holding an empty string "" matches nothing.
  • If target is the empty string, the answer is True.
  • If target is non-empty and grid has no rows or its first row is empty, the answer is False.
  • If target is longer than the number of cells (len(grid) * len(grid[0])), the answer is False.

The grid dimensions rows and cols from Job 1 have no relation to the shape of grid in Job 2.

Return value

Return a dictionary with exactly two keys:

  • "minesweeper": the grid from Job 1, as a list of lists of strings.
  • "exists": a boolean, the answer to Job 2.

Constraints

  • 1 <= rows, cols <= 50
  • 0 <= mine_count <= rows * cols
  • 1 <= len(target) <= 100 (the empty-string rule above is still applied defensively)
  • Each cell of grid is a string, possibly empty, and grid is rectangular.

Examples

Example 1

Input: build_board_and_find_word(3, 3, 2, [['A', 'B', 'C'], ['D', 'E', 'F']], 'ABED')

Output: {'minesweeper': [['M', 'M', '1'], ['2', '2', '1'], ['0', '0', '0']], 'exists': True}

Example 2

Input: build_board_and_find_word(1, 1, 0, [['A']], 'A')

Output: {'minesweeper': [['0']], 'exists': True}

Example 3

Input: build_board_and_find_word(2, 2, 4, [['A', 'B'], ['C', 'D']], 'ABCD')

Output: {'minesweeper': [['M', 'M'], ['M', 'M']], 'exists': False}

Hints

Hint 1

For the mine grid, first mark the first mine_count cells in row-major order. Cell index i maps to row i // cols and column i % cols.

Hint 2

For every non-mine cell, loop over the 8 offsets (dr, dc) except (0, 0), check bounds, and count neighbours that are 'M'. Convert the count to a string.

Hint 3

For the word search, run a DFS from every cell. At depth i the current cell must match target[i]. Keep a visited marker for the current path and clear it when you backtrack.