Count Subarrays with at Least K Equal-Fruit Pairs
Reported by candidates from TikTok's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The mistake that sinks a first attempt on this TikTok OA, reported in September 2026, is counting pairs by brute force over every subarray. With up to 10^5 fruits, that's dead on arrival. The task: count subarrays where the sum of floor(count/2) across all fruit values is at least k. It's a sliding window with a hash map, and the trick is small once you see it. If you've got the invite and 48 hours, learn the shape of it. StealthCoder sits invisibly on your screen as a safety net if you blank during the live OA, but the pattern below should get you most of the way.
The problem
You are given an integer array fruits and an integer k. A pair consists of two equal fruit values, and each array position may belong to at most one pair. For a subarray, a fruit value that appears count times contributes floor(count / 2) disjoint pairs. Return the number of contiguous subarrays whose total number of pairs across all fruit values is at least k. Function countFruitPairSubarrays(fruits: int[], k: int) → long Examples Example 1 fruits = [1,1,2,2] k = 2 return = 1 Only the complete subarray contains one pair of 1s and one pair of 2s. Example 2 fruits = [1,1,1,1] k = 1 return = 6 Every subarray of length at least two contains a pair. There are 3 + 2 + 1 = 6 such subarrays. Constraints 2 <= fruits.length <= 10^5 1 <= fruits[i] <= 10^9 1 <= k <= floor(fruits.length / 2)
Reported by candidates. Source: FastPrep
Pattern and pitfall
Pairs only grow as a window grows, so the count is monotonic. That makes two pointers work. For each right end, move left forward while the window still has at least k pairs, then every start index up to left works, so add left to the answer. Or use the standard version: for each left, find the smallest right where pairs >= k, then add n - right. Keep a hash map of counts. When you add a value and its count becomes even, pairs go up by one. When you remove a value and its count was even before removal, pairs go down by one. The common pitfall is using int for the answer. It can reach about 5*10^9, so use long. Another trap is treating pairs as distinct values instead of floor(count/2). Test with [1,1,1,1], k=1, which must return 6. StealthCoder is the hedge if the window shrink logic slips under pressure in the live OA.
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 Count Subarrays with at Least K Equal-Fruit 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 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 TikTok's OA.
TikTok 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.
Count Subarrays with at Least K Equal-Fruit Pairs FAQ
What's the trick for the TikTok fruit pairs problem?+
Pairs are monotonic. Adding elements never reduces the pair total, so a sliding window with two pointers works. Track counts in a hash map and a running pair total. When a count turns even on add, pairs rise by one. When a count was even before a removal, pairs drop by one.
Why does the answer need a long?+
The number of subarrays of an array with 10^5 elements is about 5*10^9, which overflows a 32-bit int. The function signature returns long for a reason. Accumulate into a 64-bit variable from the start, not at the end, or large tests will silently wrap.
How do I count subarrays once the window is valid?+
Fix the left index and advance right until pairs reach k. Every longer subarray from that same left also qualifies, so add n - right. Then remove fruits[left], move left forward, and repeat. Right never moves backward, so the total work is linear.
How hard is this problem really?+
It's a medium. The window idea is standard, but the pair bookkeeping trips people up. Counting floor(count/2) incrementally instead of recomputing is the part that matters. If you've done subarrays-with-at-least-K style problems, this is a small twist on that.
How do I prepare for this in 48 hours?+
Write the two-pointer solution once from scratch and run both examples by hand. Then test edge cases: all equal values, all distinct values, and k at its maximum of n/2. Focus on the add and remove pair update rules, since that's where bugs show up.