3208. Alternating Groups II
Asked at Capital One
Difficulty
Medium
Time
O(n + k)
Space
O(1)
Pattern
Array
Companies
1
Problem
You are given an array colors that describes a circle of tiles. Each value is 0 (red) or 1 (blue). You are also given an integer k.
A group is any k tiles that sit next to each other in the circle. The group is alternating when the colors keep flipping: every tile in the group has a different color from the tile right before it. In other words, no two neighbours inside the group share the same color.
Return how many alternating groups exist. Because the tiles form a circle, the last tile and the first tile are neighbours, so a group may wrap around the end of the array. Every starting tile gives exactly one group, so there are n groups in total to consider.
A circle of tiles, windows of size k
Constraints
- ●
3 <= colors.length <= 10^5 - ●
0 <= colors[i] <= 1 - ●
3 <= k <= colors.length
Examples
colors = [0,1,0,1,0], k = 33colors = [0,1,0,0,1,0,1], k = 62colors = [1,1,0,1], k = 40Asked At
1 companies| Company | Difficulty | |
|---|---|---|
| Capital One | Medium | View all Capital One questions → |
How to Think About It
Checking every window of size k directly is O(n * k).
Key insight: a window of size k ending at position i alternates iff the current alternating run ending at i has length at least k.
Maintain run: if colors[i] != colors[i-1], extend the run; otherwise reset it to 1.
For circularity, walk indices i = 1 .. n + k - 2 and read colors[i % n]. Count a window whenever i >= k - 1 and run >= k. That checks exactly n windows, one per starting tile.
Walkthrough: [0,1,0,1,0], k = 3: the run keeps growing across the linear part, but the wrap from index 4 (0) to index 0 (0) resets it. 3 windows qualify.
Optimal Approach
Step 1: run = 1, count = 0.
Step 2: For i from 1 to n + k - 2:
If colors[i % n] != colors[(i - 1) % n]: run += 1, else run = 1.
If i >= k - 1 and run >= k: count += 1.
Step 3: Return count.
Time: O(n + k). Space: O(1).
Track the current alternating run
What Trips People Up in Real Interviews
Forgetting the circular windows that wrap from the end back to the start.
Walking too far around the circle and counting some windows twice — the loop must check exactly n windows.
Copying the array to double it — fine, but indexing modulo n avoids the extra memory.
Resetting the run to 0 instead of 1 on a repeated color.
Solution Code
O(n + k) timedef numberOfAlternatingGroups(colors, k):
n = len(colors)
run = 1
count = 0
for i in range(1, n + k - 1):
if colors[i % n] != colors[(i - 1) % n]:
run += 1
else:
run = 1
if i >= k - 1 and run >= k:
count += 1
return countFrequently Asked Questions
What is the Alternating Groups II problem?
Alternating Groups II gives you a circle of red and blue tiles and asks how many groups of `k` consecutive tiles alternate in color. Because the tiles form a circle, windows can wrap around the end. Tracking the current run of alternating tiles while walking `n + k - 1` positions handles it in linear time.
How do you solve Alternating Groups II?
The optimal approach runs in O(n + k) time and O(1) space. Scroll up to "Optimal Approach" for the step-by-step walkthrough, diagrams, and code in Python, C++ and Java.
What companies ask Alternating Groups II?
Alternating Groups II is asked at Capital One. It is a medium difficulty problem.
What are common mistakes on Alternating Groups II?
- Forgetting the circular windows that wrap from the end back to the start.
- Walking too far around the circle and counting some windows twice — the loop must check exactly `n` windows.
- Copying the array to double it — fine, but indexing modulo `n` avoids the extra memory.
- Resetting the run to 0 instead of 1 on a repeated color.