Reported January 2024
ZipRecruitersliding window

Count Subarrays with K Disjoint Equal Pairs

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

The ZipRecruiter OA reported in January 2024 looks like a pairing puzzle, but it reduces to something much simpler. You count subarrays where the sum of floor(freq[x] / 2) across all values reaches k. The hinted pattern says union-find, and that's a red herring. Nothing here merges groups. It's a sliding window over a monotone count. If you've seen the "at least k" subarray counting trick, you already know the shape. If you blank under the timer, StealthCoder is the safety net running invisibly on the live OA.

The problem

For this exercise, use the callable contract below.
Given an integer array nums and a positive integer k, count contiguous subarrays that contain at least k pairwise disjoint pairs of equal values.
Each array occurrence may belong to at most one pair. Therefore, a subarray with frequencies freq[x] contains sum floor(freq[x] / 2) disjoint equal pairs.

Function
countSubarraysWithEqualPairs(nums: int[], k: int) → long

Examples
Example 1
nums = [1,1,2]
k = 1
return = 2
The qualifying subarrays are [1,1] and [1,1,2].
Example 2
nums = [1,1,1,1]
k = 2
return = 1
Only the full array contains two disjoint equal pairs.

Constraints
1 <= nums.length <= 2 * 10^5.
1 <= k <= nums.length / 2.
The answer fits in a 64-bit signed integer.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick: the pair count of a subarray never decreases when you extend it to the right, and never increases when you shrink it from the left. That monotonicity means two pointers work. For each right end r, find the largest left l such that nums[l..r] still has at least k pairs. Then every start from 0 to l is valid, so add l+1. Maintain a frequency map and a running pairs total. When you add a value, if its new frequency is even, pairs goes up by one. When you remove a value, if its old frequency was even, pairs goes down by one. Shrink while pairs stays at least k after removing the left element. The common pitfall is returning int instead of a 64-bit value, since the answer can reach about 2*10^10. Another is recomputing pairs from scratch each step, which is quadratic. StealthCoder is the hedge if the window shrink logic slips mid-assessment.

Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.

If this hits your live OA

You can drill Count Subarrays with K Disjoint Equal Pairs 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 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. 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.

Count Subarrays with K Disjoint Equal Pairs FAQ

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

Disjoint pairs in a subarray equal the sum of floor(freq / 2). That count is monotone as the window grows, so a sliding window with two pointers counts all valid subarrays in linear time. No union-find needed, despite the hint.

Why not use union-find since it's hinted?+

Union-find merges connected components, and nothing here connects. Pairs only depend on value frequencies inside a window. Forcing union-find adds complexity and gets you nowhere. Treat it as a frequency-count and window problem.

How do I update the pair count in O(1)?+

Keep a hash map of frequencies. When you add a value and its new frequency is even, increment pairs. When you remove a value and its frequency before removal was even, decrement pairs. That matches floor(freq / 2) changing by exactly one.

What return type do I need?+

A 64-bit integer. With length up to 2*10^5, the number of subarrays can be around 2*10^10, which overflows a 32-bit int. Use long in Java or C++, and Python handles it natively.

How do I prepare in 48 hours?+

Write the two-pointer count of subarrays with at least k of something from memory a couple of times. Test on [1,1,2] with k=1 (answer 2) and [1,1,1,1] with k=2 (answer 1). Watch the shrink condition and the off-by-one in adding l+1.

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