Length of Longest Subarray With at Most K Frequency
A medium-tier problem at 56% community acceptance, tagged with Array, Hash Table, Sliding Window. Reported in interviews at MakeMyTrip and 1 others.
You're grinding through a take-home or live OA and hit a sliding-window array problem that looks simple until you realize the constraint is frequency, not value. This problem appears in assessments from MakeMyTrip and Citadel, and about 56% of candidates pass it on first submission. The trick isn't complicated, but missing it means a failed test case and wasted debugging time. If you blank on the window-shrinking logic when you're live, StealthCoder surfaces a working solution invisibly so you move on.
Companies that ask "Length of Longest Subarray With at Most K Frequency"
Length of Longest Subarray With at Most K Frequency is the kind of problem that decides whether you pass. StealthCoder reads the problem on screen and surfaces a working solution in under 2 seconds. Invisible to screen share. The proctor sees nothing. Built by an Amazon engineer who used it to pass JPMorgan's OA and system design loop.
Get StealthCoderThe pattern is a standard sliding window with a hash table tracking element frequencies. Expand the window by adding elements to the right, and shrink from the left whenever any single element exceeds frequency K. The pitfall is trying to manually count or forgetting to decrement the frequency table when you shrink. Most candidates either over-complicate the frequency tracking or shrink too aggressively. The optimal approach uses a map to store count per element and one pass through the array. Time complexity is O(n) because each element enters and leaves the window once. When you're stuck mid-assessment, StealthCoder reads your problem, runs the sliding-window logic, and delivers a working submission in seconds while the proctor sees nothing.
Pattern tags
You know the problem.
Make sure you actually pass it.
Length of Longest Subarray With at Most K Frequency recycles across companies for a reason. It's medium-tier, and most candidates blank under the timer. StealthCoder is the hedge: an AI overlay invisible during screen share. It reads the problem and surfaces a working solution in under 2 seconds. Built by an Amazon engineer who used it to pass JPMorgan's OA and system design loop. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Length of Longest Subarray With at Most K Frequency interview FAQ
Is this problem still asked at FAANG or top tier companies?+
It appears in MakeMyTrip and Citadel assessments. It's a medium-difficulty window problem, so it shows up in take-homes and live OAs, not usually as a first-round screener. If you see it, expect it to be part of a multi-problem OA, not the entire interview.
What's the most common mistake candidates make?+
Forgetting to update the frequency map when shrinking the window, or shrinking until the condition is satisfied instead of shrinking correctly in one pass. Another common trap: trying to find the longest valid subarray without properly tracking which element exceeded K.
How does this differ from standard 'longest subarray with K distinct elements'?+
That problem cares about how many unique elements. This one cares about how many times any single element appears. The window logic is similar, but you're tracking frequency per element, not the count of unique elements. Same sliding-window skeleton, different guard condition.
Do I need a special data structure or will a hash map work?+
A hash map is enough. Store element to count. No heap, no segment tree, no fancy structure. The simplicity is why it's medium difficulty, not hard. The challenge is the logic, not the data structure.
How long should this take to code in a live assessment?+
If you know the pattern, 5 to 10 minutes to write and test. If you're not sure on the window shrink logic, you'll spend 15-20 minutes debugging. That's where StealthCoder helps: if you get stuck, it runs invisibly and hands you a working solution so you don't lose 30 minutes to a single problem.
Want the actual problem statement? View "Length of Longest Subarray With at Most K Frequency" on LeetCode →