Count Circular Alternating Binary Windows
Reported by candidates from ZipRecruiter's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The ZipRecruiter OA reported in September 2022 hides its trap in the wrap-around. Count Circular Alternating Binary Windows looks like a basic sliding window until the circle bites you. You have a binary array, a window length, and you count every start index where the next windowLength values strictly alternate. Most people write the window check, then watch it crawl or miss windows that cross the end. If you blank on the wrap logic mid-assessment, StealthCoder is the safety net running invisibly on your screen. Here's the trick so you don't need it.
The problem
You are given a nonempty binary array bits, treated as circular, and an integer windowLength. For every start index in the original array, inspect the next windowLength values around the circle. Count how many such windows strictly alternate between 0 and 1. Function countCircularAlternatingWindows(bits: int[], windowLength: int) → int Examples Example 1 bits = [1,0,1,1,0] windowLength = 3 return = 3 Starts 0, 3, and 4 produce alternating length-three windows. Example 2 bits = [0,1,0,1] windowLength = 4 return = 4 Every rotation of the circular array alternates for four values. Constraints 1 <= bits.length <= 100000 bits[i] is 0 or 1. 1 <= windowLength <= bits.length
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is to stop re-checking each window. Mark each position i with 1 if bits[i] != bits[(i+1) % n], else 0. A window of length k alternates exactly when its k-1 adjacent pairs are all 1s. So you need runs of 1s of length at least k-1 in the circular diff array. Use a sliding window sum over the diff array, or a running count of consecutive 1s, walking 2n steps to handle the wrap. The pitfall is windowLength = 1, where every start counts, so the answer is n. Also watch windowLength = n: the last pair must not wrap back to the first element, so you only check n-1 pairs. Brute force is O(n*k), which dies at 100000. Keep it O(n). If the wrap indexing slips on the live OA, StealthCoder can hand you a clean solution while you stay calm.
If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.
You can drill Count Circular Alternating Binary Windows 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 StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass ZipRecruiter's OA.
ZipRecruiter 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 Circular Alternating Binary Windows FAQ
What's the trick to Count Circular Alternating Binary Windows?+
Convert the array to adjacent-difference flags, where 1 means two neighbors differ. A window of length k alternates when its k-1 pairs are all 1. Then count start positions with a sliding sum or a consecutive-ones counter. This turns an O(n*k) check into O(n).
How do I handle the circular part?+
Use modulo indexing. Build the diff array with bits[i] != bits[(i+1) % n]. A window starting at s uses pairs s through s+k-2, taken modulo n. Either index with % n or conceptually double the array, but never copy it if you can avoid it.
Which edge cases break a naive solution here?+
windowLength = 1 returns n, since a single value trivially alternates. windowLength = n must check only n-1 pairs, not the wrap-around pair from last to first. A length-1 array is also valid. Test these three before submitting.
Is brute force good enough for this one?+
No. With bits up to 100000 and windowLength up to the same size, checking every window costs O(n*k), which is around 10^10 in the worst case. You need the O(n) approach using diff flags and a sliding window sum.
How do I prepare for this in 48 hours?+
Practice fixed-size sliding windows and circular indexing with modulo. Then solve a few problems where you convert a condition on windows into a condition on adjacent pairs. Walk through both examples by hand, since Example 1 tests the wrap and Example 2 tests the full-length window.