MEDIUMasked at 1 company

Count Subarrays Where Max Element Appears at Least K Times

A medium-tier problem at 63% community acceptance, tagged with Array, Sliding Window. Reported in interviews at Bloomberg and 0 others.

Founder's read

You hit this problem and suddenly the window-tracking logic breaks. Count Subarrays Where Max Element Appears at Least K Times looks straightforward until you realize the max element itself changes as the window shifts. Bloomberg asks it, and it's not rare enough to ignore. You need to count all subarrays where the maximum value appears K or more times. The naive approach (check every subarray) times out. The real trick is a two-pointer window that tracks the max correctly and knows when to shrink. If this lands in your assessment and you blank on the pattern, StealthCoder surfaces a working solution in seconds, invisible to the proctor.

Companies asking
1
Difficulty
MEDIUM
Acceptance
63%

Companies that ask "Count Subarrays Where Max Element Appears at Least K Times"

If this hits your live OA

Count Subarrays Where Max Element Appears at Least K Times 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. Made by an Amazon engineer who watched the leaked-problem repo become an industry secret. He decided you should have it too.

Get StealthCoder
What this means

The insight: use a sliding window with a frequency map that tracks how many times each element appears, plus a running max. You expand the window by adding elements to the right, and when the max element hits K occurrences, you're in a valid range. The trap is that the max can change as you shrink the left pointer, so you can't just cache it. You need a way to track or recalculate the max efficiently as the window adjusts. Some solutions use a multiset or a sorted structure; others rebuild the max on demand. Once you get the window mechanics right, the counting part is clean: for each valid right position, count how many left positions keep the subarray valid. This is a medium-difficulty problem because the window logic is trickier than standard two-pointer work, and a sloppy max-tracking approach will fail hidden test cases. When the assessment throws this at you and you freeze on the max-tracking part, StealthCoder runs invisibly and gives you the exact structure you need.

Pattern tags

The honest play

You know the problem. Make sure you actually pass it.

Count Subarrays Where Max Element Appears at Least K Times 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. Made by an Amazon engineer who watched the leaked-problem repo become an industry secret. He decided you should have it too. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Count Subarrays Where Max Element Appears at Least K Times interview FAQ

Is this really asked at big tech companies?+

Bloomberg explicitly reports this one. It's a medium problem, so it slots into the standard coding-round rotation. Not ultra-frequent, but frequent enough that if it hits your OA and you don't know the pattern, you're in trouble. It's the kind of problem that kills your speed.

What's the main trick I'm missing if I time out?+

You're probably iterating over every possible subarray and checking the max each time. O(n^3) or O(n^2 log n) won't cut it. Use a sliding window to bring it down to O(n) or O(n log n). The hard part is tracking the max element as the window shrinks, not expanding.

How does sliding window apply here differently than classic problems?+

In classic sliding window (longest substring without repeating), the constraint is symmetric: expand or shrink. Here, the max element is dynamic. You track frequencies in a map, but the identity of the max changes as you remove elements from the left. You can't just store it once at the beginning.

Will the brute force approach teach me anything useful?+

Yes. Brute force (O(n^2) checking every subarray) is correct and helps you understand the counting logic. But it won't pass the time limit. Do it first to get intuition, then optimize with a window and a data structure that tracks max efficiently, like a multiset or segment tree.

How do I handle the case where K is larger than the array length?+

If K is bigger than the array size, no subarray can have K occurrences of any element, so the answer is zero. Check this edge case early. It won't break your window logic, but it's a quick short-circuit that avoids unnecessary work.

Want the actual problem statement? View "Count Subarrays Where Max Element Appears at Least K Times" on LeetCode →

Frequency and company-tag data sourced from public community-maintained interview-report repos. Problem, description, and trademark © LeetCode. StealthCoder is not affiliated with LeetCode.