Binary String Flip and Prefix Zero Counts
Reported by candidates from ZipRecruiter's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
ZipRecruiter reportedly put this one in front of candidates in October 2023, and the input size is the whole story. Both the string and the request list can hit 100000, so flipping every bit on each flip request will time out. The question looks like string manipulation but it's really a prefix sum plus a single boolean flag. If you spot that in the first two minutes, the rest is about ten lines. And if you blank mid-OA, StealthCoder sits invisibly on your screen and hands you the approach in real time.
The problem
Maintain binaryString under requests: flip inverts every bit and emits no output. count:index appends the number of zeros from index 0 through the valid zero-based index, inclusive. Return count answers in request order. Function binaryPrefixZeroCounts(binaryString: String, requests: String[]) → int[] Examples Example 1 binaryString = "1111010" requests = ["count:4","count:6","flip","count:4","flip","count:2"] return = [1,2,4,0] Prefix counts use the current inversion state without emitting values for flips. Example 2 binaryString = "0" requests = ["count:0","flip","count:0"] return = [1,0] The one bit changes from zero to one. Constraints 1 <= binaryString.length,requests.length <= 100000
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick: never touch the string. Build a prefix array once where zeros[i] is the count of '0' characters in indices 0 through i. Keep a boolean inverted that starts false. On flip, toggle it. On count:index, the original zeros in the prefix are zeros[index]. If inverted, zeros become ones, so the answer is (index + 1) - zeros[index]. Otherwise it's zeros[index]. Every request is O(1), total O(n + q). The common pitfall is actually flipping the string on each request, which is O(n*q) and around 10^10 operations at max size. Another slip is off-by-one on the inclusive index, so the length is index + 1. Also remember flip emits nothing, so don't append to the result. Parsing is simple: split on the colon and convert the number. If the parsing or inversion logic tangles on the live OA, StealthCoder is the safety net that gives you the clean version.
Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.
You can drill Binary String Flip and Prefix Zero Counts 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 by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass ZipRecruiter's OA.
ZipRecruiter reuses patterns across OAs. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Binary String Flip and Prefix Zero Counts FAQ
What's the trick in the Binary String Flip and Prefix Zero Counts problem?+
Don't mutate the string. Precompute a prefix count of zeros, then keep one boolean for whether the string is currently inverted. Each count query becomes a lookup, either the stored zeros or the prefix length minus the stored zeros.
How hard is this ZipRecruiter OA question really?+
Easy to medium. The logic is short, but the 100000 limits punish brute force. If you know prefix sums and a lazy flag, it's a few minutes of coding. The hard part is resisting the naive flip-everything approach.
Why does brute force fail here?+
Each flip costs O(n) and you can have 100000 requests on a 100000-length string. That's roughly 10^10 operations. A prefix array with a toggle flag makes each request O(1), so the whole thing runs in linear time.
How do I compute the answer when the string is inverted?+
The prefix length is index + 1. Original zeros in that range turn into ones after inversion, and original ones turn into zeros. So the current zero count is (index + 1) minus the original zero count from your prefix array.
How do I prepare for this in 48 hours?+
Practice prefix sum problems and lazy-state tricks where you track a flag instead of updating data. Write the parser for 'count:4' style strings once. Test with both examples, including the single-bit case, so your inclusive index handling is solid.