Implement a match-3 board resolver
Company: TikTok
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: Medium
Interview Round: Technical Screen
Quick Answer: TikTok software-engineer technical-screen coding question: implement a match-3 board resolver over an m×n grid. Repeatedly remove connected horizontal/vertical runs of three or more equal cells, apply gravity so survivors fall and blanks rise to the top, cascade until the board is stable, and return the final board plus the total cells removed. It tests grid simulation, run detection via linear scans, per-column gravity, termination reasoning, and time/space complexity analysis.
Constraints
- 0 <= number of rows m <= 50
- 0 <= number of columns n <= 50
- If m > 0, every row has exactly n columns
- 0 <= board[r][c] <= 10^9
- 0 values are empty and must not be counted as matches
Examples
Input: ([])
Expected Output: [[], 0]
Explanation: An empty board is already stable and no cells are removed.
Input: ([[1,2,3],[4,5,6],[7,8,9]])
Expected Output: [[[1,2,3],[4,5,6],[7,8,9]], 0]
Explanation: There are no horizontal or vertical runs of 3 or more equal positive values.
Input: ([[1,1,1],[2,3,4],[5,6,7]])
Expected Output: [[[0,0,0],[2,3,4],[5,6,7]], 3]
Explanation: The top row of 1s is removed. Gravity leaves empty cells at the top of each column.
Input: ([[1,2,3],[1,4,5],[1,6,7],[8,9,10]])
Expected Output: [[[0,2,3],[0,4,5],[0,6,7],[8,9,10]], 3]
Explanation: The first column has a vertical run of three 1s, which are removed. The 8 falls to the bottom of that column.
Input: ([[2,1,2],[1,1,1],[3,1,4]])
Expected Output: [[[0,0,0],[2,0,2],[3,0,4]], 5]
Explanation: The middle row and middle column form an overlapping cross of 1s. The center cell is counted once, so 5 cells are removed.
Input: ([[1,2,3],[1,4,5],[7,7,7],[1,6,8]])
Expected Output: [[[0,0,0],[0,2,3],[0,4,5],[0,6,8]], 6]
Explanation: First the row of 7s is removed. Gravity then creates a vertical run of three 1s in the first column, causing a second pass. Total removed is 3 + 3 = 6.
Hints
- Detect matches first and store their positions before clearing anything; removals in the same pass must happen simultaneously.
- For gravity, process each column from bottom to top with a write pointer that tracks where the next surviving candy should fall.