PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

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.

  • Medium
  • TikTok
  • Coding & Algorithms
  • Software Engineer

Implement a match-3 board resolver

Company: TikTok

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: Medium

Interview Round: Technical Screen

##### Question Implement a match-3 board resolver (the core loop of a "candy crush"-style game). You are given an `m×n` grid of cells. Each cell holds a value identifying its type — e.g. a color character such as `'R'`/`'G'`/`'B'`, or an integer code (use whichever the interviewer specifies; the value just needs to be comparable). Repeatedly resolve the board until it stabilizes, then return the final state. On each pass: 1. **Detect matches.** Find every cell that belongs to a run of **three or more of the same value aligned horizontally or vertically** (an L/T shape counts because it is the union of a qualifying horizontal run and a qualifying vertical run — a diagonal does *not* count). Mark all such cells for removal. 2. **Remove matches.** Clear all marked cells simultaneously. 3. **Apply gravity.** Within each column, surviving cells fall straight down to fill the gaps left by removals. The emptied cells surface at the **top** of each column. State your choice for how to represent an empty cell — a blank/sentinel, or a fixed filler value such as `0`. 4. **Chain reactions.** Gravity can create new runs of three or more, so repeat steps 1–3 until a full pass produces no further removals (the board is stable). Return the **final board state** and the **total number of cells removed** across all passes. Discuss your data structures, how you detect groups, how gravity is implemented per column, the termination condition, and the time/space complexity of the whole process.

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.

Implement the core resolver loop for a match-3 game. You are given a rectangular board where each cell contains a non-negative integer. Positive integers represent candy types, and 0 represents an empty cell. Repeatedly perform passes until the board is stable. In each pass, detect every positive-valued cell that belongs to a horizontal or vertical contiguous run of 3 or more equal values. Mark all such cells, remove them simultaneously by setting them to 0, then apply gravity independently in each column so surviving candies fall downward and empty cells move to the top. Return the final stable board and the total number of cells removed across all passes. Diagonal runs do not count. L/T shapes count only because their horizontal and vertical qualifying runs are both detected; overlapping cells should be removed only once per pass.

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

  1. Detect matches first and store their positions before clearing anything; removals in the same pass must happen simultaneously.
  2. For gravity, process each column from bottom to top with a write pointer that tracks where the next surviving candy should fall.
Last updated: Jun 25, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Parse a nested list from a string - TikTok (medium)
  • Implement stacks, streaming median, and upward path sum - TikTok (easy)
  • Implement stack variants and path-sum check - TikTok (medium)
  • Solve common string/DP/stack problems - TikTok (medium)
  • Find the longest palindromic substring - TikTok (easy)