Non-Alternating Binary Partitions
Reported by candidates from DE Shaw's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The brute force here is dead on arrival: trying every way to cut a binary string into pieces blows up fast, and DE Shaw's July 2026 OA report makes the input size the whole point. You get a binary string s and a frame limit, and you need the fewest non-overlapping substrings where each one is at most frame long and not perfectly alternating. Single characters count as allowed, which changes the feel of the problem. If you blank, StealthCoder can run invisibly during the live OA as a safety net, but the idea below is small enough to hold in your head.
The problem
You are given a binary string s and an integer frame. Split s into the minimum number of non-overlapping contiguous substrings so that every substring satisfies both rules: Its length is at most frame. It is not perfectly alternating. A perfectly alternating substring changes bit at every adjacent position, such as 01, 10, or 1010. The source sample uses single-character substrings in a valid answer, so FastPrep treats a single-character substring as allowed. Function minNonAlternatingPartitions(s: String, frame: int) → int Examples Example 1 s = "101101" frame = 4 return = 3 One optimal split is 1011 | 0 | 1. The first part has length 4 and is not perfectly alternating; the source accepts the single-character pieces, giving 3 substrings.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Think of it as a minimum-cuts DP over prefixes. Let dp[i] be the fewest pieces covering the first i characters. For each i, you look back at starting points j with i - j <= frame, and a piece s[j..i) is valid if it's length 1 or it contains at least one pair of equal adjacent bits. Precompute for each position the nearest equal-adjacent pair so the validity check is O(1), which keeps the whole thing around O(n * frame) or better. The pitfall is the single-character rule. A lone bit is never alternating in the strict sense, but this problem allows it, so don't reject it. Another trap is checking alternation by scanning the substring each time, which turns it cubic. A greedy take-the-longest-piece approach can also fail, so verify it against the example 101101 with frame 4, which gives 3. If the DP transitions slip away mid-assessment, StealthCoder is your hedge.
Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.
You can drill Non-Alternating Binary Partitions 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. Made for the candidate who got the OA invite this morning and has 72 hours, not six months.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass DE Shaw's OA.
DE Shaw reuses patterns across OAs. Made for the candidate who got the OA invite this morning and has 72 hours, not six months. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Non-Alternating Binary Partitions FAQ
What's the core trick in Non-Alternating Binary Partitions?+
Prefix DP. dp[i] is the minimum pieces for the first i characters, and you try every piece ending at i with length up to frame. The only real work is validating a piece fast, which you do by tracking where the last equal adjacent pair sits.
How do I check that a substring isn't perfectly alternating quickly?+
A substring is non-alternating if it has at least one position where two neighbors match. Precompute the index of the latest equal-adjacent pair up to each position. Then a piece s[j..i) passes if that pair lies fully inside it, or if its length is 1.
Do single-character substrings count as valid here?+
Yes. The problem statement says single-character pieces are allowed, and the example split 1011 | 0 | 1 relies on it. Treat length 1 as always valid, or your answer for the sample will be wrong.
Will a greedy approach work?+
Not safely. Grabbing the longest valid piece at each step can leave a tail that forces extra cuts, while a shorter piece earlier would have saved one. DP over prefixes avoids that and is easy to reason about, so use it.
How should I prepare for this in 48 hours?+
Write the prefix-minimum DP from scratch on a couple of string partition problems, then code this one and test the example 101101 with frame 4 expecting 3. Add edge cases: frame 1, all alternating strings, and all equal bits.