MEDIUMasked at 1 company

Number of Sub-arrays With Odd Sum

A medium-tier problem at 56% community acceptance, tagged with Array, Math, Dynamic Programming. Reported in interviews at Directi and 0 others.

Founder's read

Number of Sub-arrays With Odd Sum is a medium-difficulty problem that looks deceptively simple but trips up candidates who default to brute force. You'll see it asked at companies like Directi, and the 56% acceptance rate tells you most people either TLE or miss the mathematical shortcut. The naive O(n²) scan through all subarrays feels safe until you hit a large input and watch your solution timeout. The real pattern is hidden in prefix sums and parity, not iteration. If you blank on the trick during your live assessment, StealthCoder surfaces the working solution in seconds, invisible to the proctor.

Companies asking
1
Difficulty
MEDIUM
Acceptance
56%

Companies that ask "Number of Sub-arrays With Odd Sum"

If this hits your live OA

Number of Sub-arrays With Odd Sum 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 obvious move is to generate every subarray and count those with odd sums. That works for small inputs but will fail under time pressure. The key insight is that a subarray sum is odd if and only if it contains an odd count of odd numbers. You can track this using prefix sums and parity arithmetic. Count how many prefix sums are odd versus even as you traverse the array. For each position, the number of odd-sum subarrays ending there depends on how many even-parity prefixes came before (they pair with the current odd prefix to create an odd subarray). This transforms the problem from O(n²) to O(n) with constant space, and the math is clean once you see it. Candidates who attempt DP or brute force often timeout or miscalculate edge cases. StealthCoder hedges the one case where you didn't see the parity trick before the OA started.

Pattern tags

The honest play

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

Number of Sub-arrays With Odd Sum 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.

Number of Sub-arrays With Odd Sum interview FAQ

Is this really a medium, or does the trick make it easier?+

The trick makes it trivial once you see it. But 56% acceptance suggests most people either brute force (TLE) or get lost in DP without seeing the parity pattern. If you prep for it, it's easy. If you hit it cold, it's a gotcha.

How does prefix sum relate to odd/even sums here?+

A subarray from index i to j has odd sum if prefix[j] and prefix[i-1] have different parity. Track counts of odd and even prefixes as you scan left to right. At each step, the number of odd-sum subarrays ending here equals the count of prefixes with opposite parity.

Why doesn't dynamic programming work well for this?+

DP can work but it's overkill and error-prone. You'd store odd/even counts per index, which is really just tracking prefix parity. The parity approach is simpler, faster, and less likely to have off-by-one bugs. Save DP energy for harder problems.

What's the most common mistake candidates make?+

Brute forcing all O(n²) subarrays and summing each one. Under time limits, this times out on large inputs. The second mistake is overthinking it as a DP problem when the math gives you O(n) for free.

Is this asked often at top companies?+

It's reported from Directi. It's a niche problem, not mega-frequent, but it's the type of trick question that shows up in faster-paced assessments where you need to optimize immediately. If it lands on your OA, you'll want the answer ready.

Want the actual problem statement? View "Number of Sub-arrays With Odd Sum" 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.