Reported September 2024
ZipRecruiterheap priority queue

Apply Binary State 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 September 2024, and it looks harmless until the input size hits you. You get a binary string and a list of "L" and "Cj" operations. Set the leftmost zero to 1, or force an index to 0. Simple simulation on paper. But with 100000 characters and 100000 operations, the obvious scan-from-the-left approach quietly turns quadratic. If you're staring at an OA invite for this, the trick is small and worth knowing cold. StealthCoder sits as a safety net on the live OA if your mind goes blank on the data structure.

The problem

For this exercise, assume the initial binary state is supplied as a string state. Apply the strings in operations from left to right:
"L": find the smallest index whose current value is 0 and change it to 1. If no zero remains, do nothing.
"Cj": change the value at zero-based index j to 0, regardless of its current value. The index may contain more than one digit.
Return the final binary state as a string.

Function
applyBinaryStateOperations(state: String, operations: String[]) → String

Examples
Example 1
state = "00100"
operations = ["L","C2","L"]
return = "11000"
The first load sets index 0, producing 10100. Clearing index 2 produces 10000. The final load sets the new leftmost zero at index 1.
Example 2
state = "111"
operations = ["L","C1","L"]
return = "111"
The first load has no zero to change. Clearing index 1 creates the state 101, and the final load sets that position back to 1.

Constraints
1 <= state.length <= 100000
state contains only 0 and 1.
1 <= operations.length <= 100000
Every operation is either "L" or "Cj" for a valid index 0 <= j < state.length.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The naive version rescans the string for the first zero on every "L". Worst case that's 100000 times 100000 operations, which times out. The fix is a min-heap or sorted set of zero indices. Build it from the initial state. On "L", pop the smallest index and flip it to 1. On "Cj", set the character to 0 and push j only if it wasn't already 0, otherwise you'll get duplicates. Duplicates are the classic bug, because a stale or repeated index makes "L" flip the wrong thing. Also parse j as a full integer, since "C12" is index 12, not 1 then 2. Example 2 shows the edge: "111" has no zeros, "C1" creates one, and the next "L" must find it. If you blank on the heap approach mid-assessment, StealthCoder can hand you the working solution while you keep your head clear.

The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.

If this hits your live OA

You can drill Apply Binary State 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. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.

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. 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.

Apply Binary State Operations FAQ

What's the trick in Apply Binary State Operations?+

Track the zero positions in a min-heap instead of rescanning the string. "L" pops the smallest zero index and sets it to 1. "Cj" sets index j to 0 and pushes it only if it was a 1. That brings the whole thing to O(n log n).

Why does the naive solution fail?+

Scanning for the leftmost zero on every "L" costs O(n) each time. With 100000 operations on a 100000 length string, that's around ten billion steps in the worst case. It passes small examples and then times out on the big hidden tests.

What edge cases should I test?+

Test an all-ones string with an "L" first, which must do nothing. Test clearing an index that's already 0, which must not add a duplicate to the heap. Test multi-digit indices like "C12". Finally test "C" followed by "L" on the same index, as in Example 2.

Is this a hard OA question?+

It's easy to medium. The logic is simple simulation, and the only difficulty is spotting the performance trap. If you know a heap or ordered set, it's about fifteen lines. The bugs come from duplicates and parsing, not from the algorithm.

How do I prepare in 48 hours for a ZipRecruiter OA like this?+

Practice string simulation problems where you need fast access to a minimum, using heaps or ordered sets. Write a clean operation parser that handles multi-digit numbers. Then run your solution against a worst-case input of 100000 operations to confirm it doesn't time out.

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