Reported September 2026
TikToksliding window

Count Alternating Tile Groups

Reported by candidates from TikTok's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live TikTok OA. Under 2s to a working solution.
Founder's read

The TikTok OA from September 2026 hands you a circle of red and blue tiles and asks how many windows of a given size alternate. It looks like a circular array problem, but it really reduces to counting runs of alternating adjacent pairs. Once you see that, the code is short. The trap is the wraparound and the small sizes, not the idea. If you blank mid-assessment, StealthCoder runs invisibly on your desktop and can hand you a working solution while the proctor sees nothing. Still, this one is learnable tonight.

The problem

Imagine there is a circle of red and blue tiles. The color of the tiles are represented by the array tileColors, where tileColors[i] = 0 means that the ith tile is red, whereas tileColors[i] = 1 means that the ith tile is blue.
We want to determine whether the tiles that are next to each other in the circle have alternating colors. The ith tile should have a different color than both the i+1th and the i-1th neighboring tiles. Given an integer size, determine how many groups of size consecutive tiles have alternating colors.
Note: Because tileColors represents a circle, the first and last tiles are considered to be next to each other.
Note: You are not expected to provide the most optimal solution, but a solution with time complexity not worse than O(tileColors.length2) will fit within the execution time limit.

Function
solution(tileColors: int[], size: int) → int

Examples
Example 1
tileColors = [0, 1, 0, 1, 1]
size = 3
return = 3
There are five unique groups of size 3:
tileColors[0...2] = [0, 1, 0], which has alternating colors.
tileColors[1...3] = [1, 0, 1], which has alternating colors.
tileColors[2...4] = [0, 1, 1], which does not have alternating colors.
tileColors[3...4] + tileColors[0] = [1, 1, 0], which does not have alternating colors.
tileColors[4] + tileColors[0...1] = [1, 0, 1], which has alternating colors.
There are 3 groups that have alternating colors.

Constraints
Each element of tileColors is either 0 (red) or 1 (blue).
tileColors contains at least 1 element.
1 <= size <= tileColors.length
A solution with time complexity O(tileColors.length2) or better is sufficient.

Reported by candidates. Source: FastPrep

Pattern and pitfall

Here's the trick. Define diff[i] = 1 if tileColors[i] != tileColors[(i+1) % n]. A window of size k is alternating exactly when its k-1 consecutive diff values are all 1. So you're counting windows of length k-1 in a circular diff array that are all ones. Walk the indices 0 to n-1 as window starts, and keep a running count of consecutive ones, or just use a sliding window sum over the diff array extended by k-1 elements. The O(n*k) brute force is allowed here, so don't overthink it. Pitfalls: forgetting modulo on the wraparound, and mishandling size = 1, where every tile is a valid group, so the answer is n. Also remember there are exactly n starting positions, not n-k+1. If you freeze during the live OA, StealthCoder is the safety net, but the diff-array idea is the whole problem.

If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.

If this hits your live OA

You can drill Count Alternating Tile Groups cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken.

Get StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as alternating groups i. If you have time before the OA, drill that.

⏵ The honest play

You've seen the question. Make sure you actually pass TikTok's OA.

TikTok reuses patterns across OAs. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Count Alternating Tile Groups FAQ

How hard is Count Alternating Tile Groups really?+

Easy to medium. The brute force is explicitly accepted, since O(n^2) fits the limit. The only real difficulty is the circular wraparound. If you can write a modulo index loop and check adjacent pairs, you can pass this one.

What's the trick to solve it fast?+

Turn it into adjacent-pair checks. A window of size k alternates when all k-1 neighboring pairs inside it differ. Precompute whether each pair differs, then count windows where all those pairs are true. Sliding window or a running streak both work.

How do I handle the circular part?+

Use index % n for every access. There are exactly n starting positions, one per tile, because the circle has no ends. Each window starting at i covers i through i+k-1 modulo n. Don't stop at n-k like you would on a flat array.

What edge cases break most solutions?+

Size 1 is the big one. A single tile has no neighbors inside the window, so every tile counts and the answer is n. Also test size equal to n, where the window wraps the whole circle, and arrays of length 1 or 2.

How do I prepare for this in 48 hours?+

Write the brute force first and confirm it matches the example, which returns 3. Then write the O(n) version using a diff array and compare outputs on random small inputs. Practice circular indexing on two or three similar problems. That's enough for this question.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with TikTok.

OA at TikTok?
Invisible during screen share
Get it