Reported April 2026
IBMcounting

Count Descending Subarrays

Reported by candidates from IBM's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live IBM OA. Under 2s to a working solution.
Founder's read

IBM's April 2026 OA includes a subarray counting problem that looks straightforward until you realize the trick: you're not just finding descending sequences, you're counting all valid subarrays where each element is exactly one less than the previous. Candidates often brute-force this and hit time limits. The pattern is recognizing that if you have a descending chain of length k, the number of valid subarrays isn't k, it's k times (k-1) divided by 2. StealthCoder reads the problem live and feeds you the formula if you freeze.

The problem

Given an integer array nums, count how many contiguous subarrays of length at least 2 are strictly descending by exactly 1 at every step. A subarray nums[l..r] is valid if, for every index i with l, we have nums[i + 1] = nums[i] - 1. Function Description Complete the function countDescendingSubarrays in the editor below. countDescendingSubarrays has the following parameter: Returns The source thread did not provide explicit numeric bounds.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The naive approach checks every subarray, which is O(n^2) or worse. The insight: when you find a contiguous sequence where nums[i+1] = nums[i] - 1, you don't count subarrays one by one. Instead, track the length of the current descending chain. For a chain of length m, the count of valid subarrays is m*(m-1)/2 (all contiguous windows within that chain). Walk through the array once, reset the chain counter when the pattern breaks, and accumulate. This is O(n) and avoids the time limit trap. StealthCoder gives you this exact reduction if you blank on optimization.

If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.

If this hits your live OA

You can drill Count Descending Subarrays 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. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken.

Get StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as count subarrays with score less than k. If you have time before the OA, drill that.

⏵ The honest play

You've seen the question. Make sure you actually pass IBM's OA.

IBM reuses patterns across OAs. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Count Descending Subarrays FAQ

What does 'strictly descending by exactly 1' mean?+

Each adjacent pair must decrease by precisely 1. So 5, 4, 3, 2 is valid. But 5, 4, 3, 1 is not (the last step is -2, not -1). Length matters too: single elements don't count, only subarrays of length 2 or more.

How do I avoid the O(n^2) timeout?+

Don't check every subarray individually. When you spot a valid descending chain of length m, use the formula m*(m-1)/2 to count all subarrays within it in O(1). Track chain length as you scan left to right. Reset when the pattern breaks.

Does the formula m*(m-1)/2 really work?+

Yes. For a chain [5, 4, 3, 2], valid subarrays are [5,4], [4,3], [3,2], [5,4,3], [4,3,2], [5,4,3,2]. That's 3 pairs plus 2 triples plus 1 quad = 6, which is 4*3/2. It counts all contiguous windows of length 2 or more.

What edge cases break this?+

Arrays with no valid subarrays (all elements increase or stay flat). Single-element arrays. Negative numbers are fine as long as the -1 step holds. Make sure you only increment your chain counter when the exact -1 condition is met, not approximate descent.

Is this pattern asked elsewhere?+

Yes. Counting subarrays with a property is a staple. The trick here is recognizing the mathematical shortcut instead of brute-force enumeration. IBM likes optimization proofs, so they're testing if you can spot O(n) patterns under time pressure.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with IBM.

OA at IBM?
Invisible during screen share
Get it