Valid Binary Prefix
Reported by candidates from Goldman Sachs's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Goldman Sachs reported this one in June 2026, and it looks scarier than it is. Strip the wording and it reduces to counting "10" subsequences in each prefix and checking one inequality. You're taking the OA soon, so here's the shape: scan left to right, track ones seen and the running count of "10" pairs, and decide per prefix whether k is still reachable. If you blank on the live assessment, StealthCoder is the quiet hedge, but the logic below is short enough to hold in your head.
The problem
Given a binary sequence of length n as a string, perform the following operation any number of times: Append either a '0' or a '1' to the end of the sequence. A sequence is considered valid if it is possible to make the total number of "10" subsequences in the updated sequence exactly equal to k. Your task is to count the total number of valid non-empty prefix sequences of the given binary sequence. Notes: A sequence is a subsequence if it can be obtained by deleting digits, possibly none, without altering the relative positions. A non-empty prefix sequence is any sequence derived by deleting digits from the end of the given sequence, ensuring the length of the prefix is at least 1. Function countValidBinaryPrefixes(sequence: String, k: int) → int Examples Example 1 sequence = "100" k = 1 return = 2 Analyze all non-empty prefix sequences: Prefix SequenceAppended StringSequence Formed After AppendIs Good? "1""01""101"Good (There are k "10" sequences) "10"Blank"10"Good (There are k "10" sequences) "100"----Not Good (Not possible) For "100", there are already 2 subsequences of "10": indices (0, 1) and indices (0, 2). Hence, the number of non-empty prefix sequences of the given binary sequence is 2. Return 2.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is that appending only adds to the "10" count, never removes. So a prefix is valid only if its current count of "10" subsequences is at most k. Appending a '0' adds one "10" for every '1' already present, and appending '1' adds nothing. So you can raise the count by multiples of the ones count, and you can always append 1s to stall. Walk the string once. Keep ones, and pairs, where each '0' adds ones to pairs. For each prefix, check pairs <= k, then check that the gap k - pairs can be filled by appending zeros: either the gap is 0, or ones > 0 and gap % ones == 0. The pitfall is the case with no ones, where pairs stays 0 and only k == 0 works. Another is overflow on pairs, so use a long. Once pairs exceeds k, you can stop. That's O(n) time and O(1) space. Check example 1 by hand before submitting. StealthCoder can cover you if the edge cases slip.
The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.
You can drill Valid Binary Prefix 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 for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Goldman Sachs's OA.
Goldman Sachs reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Valid Binary Prefix FAQ
What's the actual trick in Valid Binary Prefix?+
The "10" count only goes up when you append. So track ones and the running pair count per prefix. A prefix is valid if pairs <= k and the remaining gap is zero or divisible by the number of ones, since each appended '0' adds exactly that many pairs.
How hard is this Goldman Sachs OA question really?+
Easy to medium. The code is a single pass with two counters. The difficulty is the reasoning about what appending can reach, especially the divisibility check and the zero-ones edge case. Once you see that, it takes minutes to write.
What edge cases break most solutions?+
A prefix with no ones, where the pair count is stuck at 0 so only k == 0 is valid. Also a gap of zero, which is always good. And overflow: pairs can reach roughly n squared over 4, so use a 64-bit integer.
Can I stop scanning early once a prefix fails?+
Yes, for the pairs > k failure. The count never decreases as the prefix grows, so every longer prefix also fails. Break out and return the tally. A prefix failing only the divisibility check doesn't stop the scan, since longer ones may pass.
How do I prepare for this in 48 hours?+
Practice counting subsequences with running counters, like counting "01" or "10" pairs in one pass. Then hand-trace example 1 and write a few tiny cases with k = 0. You don't need heavy data structures here, just clean counter logic.