Longest Frequency-Bounded Subarray
Reported by candidates from ZipRecruiter's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The ZipRecruiter OA reported in October 2024 hands you an array up to 100000 long and asks for the longest contiguous stretch where no value shows up more than k times. That size kills brute force before you type a line. Checking every subarray is O(n^2) or worse, and it times out. The hint says dynamic-programming, but this is really a sliding window with a frequency map. If you've got an invite and 48 hours, learn this shape cold. StealthCoder sits invisible on your screen as a safety net if you blank during the live OA.
The problem
Return the maximum length of a contiguous subarray in which no distinct value occurs more than k times. Function longestFrequencyBoundedSubarray(values: int[], k: int) → int Examples Example 1 values = [1,1,2,3] k = 1 return = 3 The reported maximum follows the per-value frequency bound. Example 2 values = [1,2,1,3,2] k = 1 return = 3 The reported maximum follows the per-value frequency bound. Constraints 1 <= values.length <= 100000 1 <= k <= values.length
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick: keep a window [left, right] and a hash map of counts. Move right forward and increment the count of values[right]. If that count goes above k, shrink from the left, decrementing counts, until the offending value is back to k. Then record right - left + 1 as a candidate. Each index enters and leaves the window once, so it's O(n) time and O(n) space. The common pitfall is resetting the whole map when you break the rule, or only shrinking once instead of in a while loop. Another is checking all values instead of just the one you added. Only the new value can break the bound. Test with [1,1,2,3], k=1: the answer is 3, from [1,2,3]. If you freeze mid-assessment, StealthCoder can read the problem and give you the window code as a hedge.
Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.
You can drill Longest Frequency-Bounded Subarray 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 StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as length of longest subarray with at most k frequency. If you have time before the OA, drill that.
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.
Longest Frequency-Bounded Subarray FAQ
What's the trick for the ZipRecruiter frequency-bounded subarray problem?+
Use a sliding window with a count map. Expand right, and when the newly added value exceeds k occurrences, move left forward until that value's count drops back to k. Track the max window size. It runs in O(n) because each index is added and removed at most once.
Is this really dynamic programming?+
The hint says so, but you don't need a DP table. The constraint is on a contiguous window, so two pointers with a hash map is simpler and faster. Don't waste your 48 hours building a DP state that the sliding window makes unnecessary.
Why does brute force fail here?+
With length up to 100000, checking every subarray is O(n^2) pairs, and validating each one adds more work. That's billions of operations. You need a single pass, which is what the sliding window gives you.
What edge cases should I test?+
Test k equal to the array length, where the whole array is valid. Test all identical values with k=1, where the answer is 1. Test a single-element array. Also run both examples: [1,1,2,3] with k=1 gives 3, and [1,2,1,3,2] with k=1 gives 3.
How do I prepare for this in 48 hours?+
Write the sliding window with a count map from scratch three times. Practice the shrink-while-invalid loop until it's automatic. Then try variants like at most k distinct values. The same template covers most window problems, so you're learning one shape, not many.