Solve message splitting and board crushing
Company: TikTok
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: Medium
Interview Round: Technical Screen
Quick Answer: These problems evaluate string manipulation and numeric reasoning for variable-length metadata alongside grid simulation with pattern detection and gravity effects, testing skills such as careful edge-case handling, iterative state updates, and complexity analysis.
Split Message Based on Limit
Constraints
- 1 <= message.length <= 10^4
- message consists of only lowercase English letters and spaces (per LeetCode)
- 1 <= limit <= 10^4
Examples
Input: ("this is really a very awesome message", 9)
Expected Output: ['thi<1/14>', 's i<2/14>', 's r<3/14>', 'eal<4/14>', 'ly <5/14>', 'a v<6/14>', 'ery<7/14>', ' aw<8/14>', 'eso<9/14>', 'me<10/14>', ' m<11/14>', 'es<12/14>', 'sa<13/14>', 'ge<14/14>']
Explanation: 14 parts. Parts 1-9 have single-digit indices (suffix length 5, content 4); parts 10-14 have two-digit indices (suffix length 6, content 3). Together they exactly cover the 37-character message.
Input: ("short message", 15)
Expected Output: ['short mess<1/2>', 'age<2/2>']
Explanation: With b=1 the single suffix "<1/1>" leaves only 10 content chars < 13, so b=1 fails. b=2 gives 10 content chars per part (20 total >= 13); the message splits into 'short mess' + 'age'.
Input: ("a", 6)
Expected Output: ['a<1/1>']
Explanation: Single character fits in one part: 'a' + '<1/1>' = 6 chars = limit.
Input: ("abc", 5)
Expected Output: []
Explanation: Impossible. Any suffix "<i/b>" is at least 5 characters, leaving 0 content room when limit is 5, so the message can never be placed.
Hints
- The suffix "<a/b>" costs 3 fixed characters plus the digit-lengths of a and b. Once you fix the total number of parts b, the suffix cost per part is fully determined.
- Iterate over the candidate number of parts b from small to large. For each b, compute the total content capacity = sum over i of (limit - 3 - len(str(i)) - len(str(b))). The first b whose capacity >= len(message) AND that exactly consumes the message is the answer.
- Any part with non-positive available content space makes that b infeasible — skip it.
- Because you try b in increasing order, the first feasible b is automatically the minimum.
Candy Crush
Constraints
- m == board.length
- n == board[i].length
- 3 <= m, n <= 50
- 1 <= board[i][j] <= 2000 or board[i][j] == 0
Examples
Input: ([[110,5,112,113,114],[210,211,5,213,214],[310,311,3,313,314],[410,411,412,5,414],[5,1,512,3,3],[610,4,1,613,614],[710,1,2,713,714],[810,1,2,1,1],[1,1,2,2,2],[4,1,4,4,1014]],)
Expected Output: [[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [110, 0, 0, 0, 114], [210, 0, 0, 0, 214], [310, 0, 0, 113, 314], [410, 0, 0, 213, 414], [610, 211, 112, 313, 614], [710, 311, 412, 613, 714], [810, 411, 512, 713, 1014]]
Explanation: The canonical LeetCode 723 example. A column of 1s and other vertical/horizontal triples crush, gravity drops the survivors, and after cascades the board stabilizes with three fully-empty top rows.
Input: ([[1,3,5,5,2],[3,4,3,3,1],[3,2,4,5,2],[2,4,4,5,5],[1,4,4,1,1]],)
Expected Output: [[1, 3, 0, 0, 0], [3, 4, 0, 5, 2], [3, 2, 0, 3, 1], [2, 4, 0, 5, 2], [1, 4, 3, 1, 1]]
Explanation: Vertical run of 4s in column 2 crushes; after gravity the rest of the board is stable with that column partly emptied at the top.
Input: ([[5,5,5]],)
Expected Output: [[0, 0, 0]]
Explanation: A single row of three identical candies is a horizontal run of 3 and crushes to all zeros.
Input: ([[1]],)
Expected Output: [[1]]
Explanation: A 1x1 board can never have a run of 3; it is already stable.
Input: ([[2,2],[2,2]],)
Expected Output: [[2, 2], [2, 2]]
Explanation: Although there are four 2s, no run reaches length 3 in any single row or column, so the board is stable as-is.
Hints
- Scan separately for horizontal and vertical runs of 3+ identical candies. Use a marking pass so all crushes are detected from the SAME board state before any cell is zeroed (simultaneous crushing).
- A clean way to mark: collect all (row, col) coordinates that belong to any run of length >= 3 into a set, then zero them all at once.
- After zeroing, apply gravity column by column: write surviving (non-zero) values from the bottom up, then fill the remaining top cells with 0.
- Repeat the whole detect-crush-gravity cycle until a full pass finds nothing to crush — that signals the board is stable.