MEDIUMasked at 3 companies

Count Number of Nice Subarrays

A medium-tier problem at 73% community acceptance, tagged with Array, Hash Table, Math. Reported in interviews at Roblox and 2 others.

Founder's read

Count Number of Nice Subarrays is a medium-difficulty array problem that's been asked at Roblox, Deliveroo, and Walmart Labs. You're given an array of integers and need to count subarrays where exactly k numbers are odd. On the surface it looks like brute force, but the twist is the constraint makes naive approaches time out. The 73% acceptance rate tells you most people can code something, but many hit subtle bugs in edge cases or choose an inefficient pattern. If you blank on the transformation that turns this into a prefix-sum or sliding-window problem during your live OA, StealthCoder surfaces a working solution in seconds, invisible to the proctor.

Companies asking
3
Difficulty
MEDIUM
Acceptance
73%

Companies that ask "Count Number of Nice Subarrays"

If this hits your live OA

Count Number of Nice Subarrays 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 a working FAANG engineer who treats the OA the way companies treat hiring: as a game with rules you should know.

Get StealthCoder
What this means

The key insight is reframing odd numbers as 1 and even numbers as 0, then counting subarrays with exactly k ones. Most candidates jump to nested loops and get TLE. The real trick is converting 'exactly k' into a subtraction: count(at most k) minus count(at most k-1). From there you can use a two-pointer sliding window to find the longest subarray with at most k odd numbers, or use prefix sums with a hash table to track cumulative counts. The sliding-window approach is cleaner once you see it. Common pitfall: forgetting to handle the boundary between 'at most k' and 'exactly k', or miscounting when the window shrinks. Hash Table and Prefix Sum are both viable routes. If the pattern doesn't click during your assessment and you're running out of time, StealthCoder runs in the background and hands you the code without the proctor knowing.

Pattern tags

The honest play

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

Count Number of Nice Subarrays 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 a working FAANG engineer who treats the OA the way companies treat hiring: as a game with rules you should know. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Count Number of Nice Subarrays interview FAQ

Is this really a medium problem?+

The 73% acceptance rate and medium tag are justified. Brute force works on small inputs, but the 'exactly k' constraint requires an optimization insight. Once you see the 'at most k minus at most k-1' trick or the sliding-window pattern, it's straightforward. Most people can code it, but pattern recognition is the gating factor.

Do Roblox and Deliveroo actually ask this?+

Yes. Both appear in recent OA reports for this problem. They're testing whether you can transform a count problem and apply efficient algorithms. It's a screening filter for coding fluency, not a trick question meant to stump you.

What's the fastest approach?+

Sliding window with two pointers. Keep a window of at most k odd numbers, slide the right pointer, and shrink the left when needed. For each right position, the number of valid subarrays ending there is the width of the valid window. O(n) time, O(1) space.

Why does the prefix-sum approach work?+

Track cumulative count of odd numbers up to each index. For each position, count how many prior positions have a cumsum exactly k less than the current one. That's your subarray count. Hash table stores cumsum frequencies. Also O(n), slightly higher space. Both work; sliding window is usually faster in practice.

What's the biggest gotcha?+

Off-by-one errors in window boundaries and forgetting to count all valid subarrays for each right pointer. Also, some candidates try to track odd/even manually and lose track of indices. Once you abstract to 'at most k ones', the bugs disappear.

Want the actual problem statement? View "Count Number of Nice Subarrays" 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.