Implement Minesweeper and Word Search
Company: Uber
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
Quick Answer: This multi-part prompt evaluates grid-based algorithmic skills, including randomized board construction with neighbor counting, straight-line pattern matching across eight directions, and path-based word search using horizontally/vertically adjacent cells.
Part 1: Generate a Minesweeper Board from Given Mine Positions
Constraints
- 1 <= rows, cols <= 200
- 0 <= len(mine_positions) <= rows * cols
- Each mine position must be a distinct pair (r, c) with 0 <= r < rows and 0 <= c < cols
- Return [] for invalid dimensions, duplicates, malformed coordinates, or out-of-bounds positions
Examples
Input: (3, 4, [(0, 2), (2, 3)])
Expected Output: [['0', '1', '*', '1'], ['0', '1', '2', '2'], ['0', '0', '1', '*']]
Explanation: The counts are based on the two mines at positions (0, 2) and (2, 3).
Input: (1, 1, [(0, 0)])
Expected Output: [['*']]
Explanation: Single-cell board containing a mine.
Input: (2, 2, [])
Expected Output: [['0', '0'], ['0', '0']]
Explanation: No mines means every cell has zero adjacent mines.
Input: (2, 2, [(0, 0), (0, 0)])
Expected Output: []
Explanation: Duplicate mine coordinates are invalid.
Hints
- Place all mines first, then update the 8 neighbors of each mine.
- Use a set to detect duplicate mine coordinates quickly.
Part 2: Search for a Word in a Straight Line
Constraints
- 0 <= number of rows, number of cols <= 200
- The grid is rectangular if non-empty
- 0 <= len(word) <= 10^4
- Grid cells contain single-character strings
Examples
Input: ([['A', 'B', 'C', 'E'], ['S', 'F', 'C', 'S'], ['A', 'D', 'E', 'E']], 'ABC')
Expected Output: True
Explanation: ABC appears horizontally from left to right in the first row.
Input: ([['C', 'A', 'T'], ['X', 'A', 'X'], ['T', 'A', 'C']], 'CAC')
Expected Output: True
Explanation: CAC appears diagonally from the top-left to the bottom-right.
Input: ([['A', 'B', 'C', 'E'], ['S', 'F', 'C', 'S'], ['A', 'D', 'E', 'E']], 'ABCC')
Expected Output: False
Explanation: A match would require a turn, which is not allowed.
Input: ([['Z']], 'Z')
Expected Output: True
Explanation: A single matching cell is a valid straight-line match.
Input: ([], 'A')
Expected Output: False
Explanation: A non-empty word cannot be found in an empty grid.
Hints
- Only start searching from cells that match the first letter.
- For each starting cell, try all 8 directions and check whether the whole word stays in bounds.
Part 3: Search for a Word by Adjacent Cells
Constraints
- 0 <= number of rows, number of cols <= 6
- The grid is rectangular if non-empty
- 0 <= len(word) <= 15
- Grid cells contain single-character strings
Examples
Input: ([['A', 'B', 'C', 'E'], ['S', 'F', 'C', 'S'], ['A', 'D', 'E', 'E']], 'ABCCED')
Expected Output: True
Explanation: The word can be formed by moving through adjacent cells without reusing any cell.
Input: ([['A', 'B', 'C', 'E'], ['S', 'F', 'C', 'S'], ['A', 'D', 'E', 'E']], 'SEE')
Expected Output: True
Explanation: A valid path exists using adjacent cells.
Input: ([['A', 'B', 'C', 'E'], ['S', 'F', 'C', 'S'], ['A', 'D', 'E', 'E']], 'ABCB')
Expected Output: False
Explanation: The word would require reusing a cell, which is not allowed.
Input: ([['A']], 'A')
Expected Output: True
Explanation: Single-cell board with a matching one-letter word.
Input: ([['A', 'B'], ['C', 'D']], '')
Expected Output: True
Explanation: The empty word is always considered found.
Hints
- This is a depth-first search with backtracking: mark a cell as used while exploring, then restore it.
- Before searching, compare character counts in the board and in the word to quickly reject impossible cases.