Implement BFS and a Job Scheduler
Company: Nuro
Role: Backend Engineer
Category: Coding & Algorithms
Difficulty: hard
Interview Round: Onsite
Quick Answer: Solve grid reveal with BFS and design a thread-safe periodic job scheduler. The solution covers Minesweeper-style expansion, queue processing, priority-queue scheduling, locks, worker pools, cancellation, duplicate prevention, long-running tasks, clock drift, and backpressure.
Constraints
- 1 <= m, n <= 50
- board[i][j] is one of 'M', 'E', 'B', 'X', or a digit '1'-'8'
- click.length == 2
- 0 <= click[0] < m and 0 <= click[1] < n
- board[click[0]][click[1]] is either 'M' or 'E' (the clicked square is unrevealed)
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: Click on (3,0), a blank far from the mine. BFS floods all reachable empties, marking 'B' for cells with no adjacent mine and the digit '1' for the ring of cells touching the single mine. The mine and the two cells fully shielded from the flood by digit cells stay 'M'/'E'.
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: Clicking the mine at (1,2) ends the game: 'M' becomes 'X' and the board is returned immediately with no further reveals.
Input: ([['M']], [0, 0])
Expected Output: [['X']]
Explanation: A 1x1 board whose only cell is a mine. Clicking it turns 'M' into 'X'.
Input: ([['E','M'],['E','E']], [1, 0])
Expected Output: [['E','M'],['1','E']]
Explanation: Cell (1,0) borders exactly one mine (at (0,1)), so it becomes '1' and does NOT spread. The remaining 'E' cells stay unrevealed.
Input: ([['E','E'],['E','E']], [0, 0])
Expected Output: [['B','B'],['B','B']]
Explanation: No mines anywhere, so the flood from (0,0) reveals every cell as a blank 'B'.
Hints
- First handle the special case: if the clicked cell is a mine 'M', turn it into 'X' and return immediately.
- Otherwise the clicked cell is 'E'. Count mines in the 8 neighboring cells. If the count is > 0, write that digit and stop spreading; if it's 0, mark it 'B' and flood-fill.
- Use a BFS queue plus a visited set so each cell is enqueued at most once. Only enqueue neighbors that are still 'E' — never spill across a digit cell into a region it doesn't border.
- Process the 8 directions for both mine-counting and neighbor expansion; reuse the same direction list for both.