Reported October 2022
ZipRecruitersimulation

Count Prioritized Binary-Run Operations

Reported by candidates from ZipRecruiter's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live ZipRecruiter OA. Under 2s to a working solution.
Founder's read

ZipRecruiter reported this one in October 2022, and the input size is the first thing to read. The string can hit 100000 characters, so building and editing it literally is the trap. Prepending a 1 and chopping zeros off the end means O(n) work per operation, and that dies fast. The string is always some 1s followed by some 0s, so it's really just two counts. This is a simulation problem in disguise, with a bit of math to batch the steps. If you blank on the count bookkeeping during the live OA, StealthCoder runs invisibly as a safety net and reads the problem for you. But the idea is small enough to hold in your head.

The problem

bits consists of zero or more 1s followed by zero or more 0s, and k >= 2. Repeatedly apply the first available rule:
If at least k zeros exist, remove the last k zeros and prepend one 1.
Otherwise, if a 1 exists, replace the last 1 with 0.
Stop when neither rule applies and return the number of operations.

Function
countBinaryOperations(bits: String, k: int) → long

Examples
Example 1
bits = "10"
k = 2
return = 3
The states by counts are (1,1), (0,2), (1,0), and (0,1).
Example 2
bits = "00"
k = 2
return = 2
Compress two zeros to one 1, then convert that 1 to one zero.

Constraints
1 <= bits.length <= 100000
2 <= k <= 100000
The operation count fits in signed 64-bit.

Reported by candidates. Source: FastPrep

Pattern and pitfall

Track (ones, zeros) and never touch the string. Rule one: if zeros >= k, zeros -= k and ones += 1. Rule two, only when zeros < k: ones -= 1 and zeros += 1, since the last 1 becomes the first 0. Stop when ones is 0 and zeros < k. Check it against the samples: (1,1) goes to (0,2), then (1,0), then (0,1), which is 3 operations. The first-available-rule order matters, so always test the compress rule before the convert rule. Every full cycle turns k-1 net ones into zeros at a cost of about k+1 operations, so the total stays bounded and a count-based loop runs fast. The pitfall is using int. Accumulate the answer in a 64-bit type, and compress the initial zeros up front. If the cycle math fights you live, StealthCoder is the hedge, but a plain two-variable loop passes.

StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.

If this hits your live OA

You can drill Count Prioritized Binary-Run Operations 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. If you're reading this with an OA window open, you're who this was built for.

Get StealthCoder

Related leaked OAs

⏵ The honest play

You've seen the question. Make sure you actually pass ZipRecruiter's OA.

ZipRecruiter reuses patterns across OAs. If you're reading this with an OA window open, you're who this was built for. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Count Prioritized Binary-Run Operations FAQ

What's the trick in this ZipRecruiter OA problem?+

Stop treating it as a string. The input is always 1s then 0s, so two integers capture the whole state. Apply the compress rule first whenever zeros >= k, otherwise convert one 1 to a 0. Count each step. Nothing fancier is needed.

Will a direct simulation time out with length up to 100000?+

Not if you simulate counts instead of characters. Each operation is O(1) on two integers, and the total operation count stays roughly linear in the starting counts. Simulating the actual string with prepends and removals is what blows up.

Why does the answer need a 64-bit return type?+

The statement says the operation count fits in a signed 64-bit integer, and the function returns a long. Use long or long long for the accumulator. Counts like ones and zeros fit in ints, but the running total shouldn't be assumed to.

What edge cases should I test before submitting?+

Run both samples: bits "10" with k=2 gives 3, and "00" with k=2 gives 2. Also try all ones, all zeros, and a zero count just below k. Confirm the compress rule gets checked before the convert rule on every step.

How do I prepare for this in 48 hours?+

Write the two-counter loop from scratch twice and trace the samples by hand. Then practice spotting when a string problem collapses into counts. It's a short problem once you see that, so spend the rest of your time on reading constraints carefully.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with ZipRecruiter.

OA at ZipRecruiter?
Invisible during screen share
Get it