Reported October 2026
Microsoftdynamic programming

Count Nondecreasing Digit-Sum Arrays

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

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

Microsoft reported this one in October 2026, and the detail that matters is in Example 3: the only value up to 5000 with digit sum 31 is 4999. That tells you the candidate pool per index is tiny and sorted. It's a counting DP over a nondecreasing chain, dressed up as a digit-sum problem. If you've got an OA invite for this week, the structure is simpler than the statement looks. Group values 1 to 5000 by digit sum, then count chains. And if you blank under the timer, StealthCoder sits invisibly on your screen as a safety net and hands you the DP.

The problem

Given an integer array requiredSum of length n, count the arrays result of length n that satisfy all of the following rules:
result is nondecreasing, so result[i] <= result[i + 1] for every valid i.
For every index i, the sum of the decimal digits of result[i] equals requiredSum[i].
Every value in result is between 1 and 5000, inclusive.
Return the number of distinct valid arrays modulo 10^9 + 7.

Function
countDigitSumArrays(requiredSum: int[]) → int

Examples
Example 1
requiredSum = [1]
return = 4
The valid one-element arrays are [1], [10], [100], and [1000].
Example 2
requiredSum = [1,1]
return = 10
The only eligible values are 1, 10, 100, and 1000. Choosing any two of them with repetition and writing them in nondecreasing order gives 10 arrays.
Example 3
requiredSum = [31,1]
return = 0
The only value at most 5000 with digit sum 31 is 4999. No value greater than or equal to 4999 has digit sum 1, so no valid nondecreasing array exists.

Constraints
1 <= requiredSum.length <= 5000.
1 <= requiredSum[i] <= 31.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick: precompute digit sums for every value from 1 to 5000. For each index i, the candidates are the values whose digit sum equals requiredSum[i], in sorted order. Then dp[i][v] is the number of valid arrays ending at value v at position i. dp[i][v] equals the sum of dp[i-1][u] for all u <= v, so use a running prefix sum as you sweep v upward. That makes each layer O(5000) instead of quadratic. Total work is about n times 5000, which is fine for n up to 5000. Pitfalls: forgetting the modulo 10^9 + 7 on every addition, resetting dp for values that don't match the digit sum, and allowing a strictly increasing chain by mistake. Equal values are allowed. If you freeze live, StealthCoder is the hedge that surfaces the prefix-sum DP so you just type it out.

The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.

If this hits your live OA

You can drill Count Nondecreasing Digit-Sum Arrays 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 for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.

Get StealthCoder

Related leaked OAs

⏵ The honest play

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

Microsoft reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Count Nondecreasing Digit-Sum Arrays FAQ

What's the trick in Count Nondecreasing Digit-Sum Arrays?+

Treat it as a DP over positions and values. Only values with the right digit sum are allowed at each index. Use a prefix sum over the previous layer so each transition is constant time. Nondecreasing means you sum all earlier values less than or equal to the current one.

How hard is this Microsoft OA question really?+

Medium. The statement is wordy, but the solution is a standard counting DP with prefix sums. The small value range of 1 to 5000 keeps memory and time manageable. Most people stumble on the transition, not the digit-sum part.

What's the time complexity I should aim for?+

Roughly O(n * 5000) with prefix sums, which handles n up to 5000. A naive transition summing over all smaller values per cell is O(n * 5000^2) and will be too slow. Keep only two rolling arrays to save memory.

Why does Example 3 return 0?+

A digit sum of 31 under 5000 only allows 4999. The next index needs digit sum 1, and every value with digit sum 1 is below 4999 (1, 10, 100, 1000). Nothing can follow 4999 in a nondecreasing array, so the count is zero.

How do I prepare for this in 48 hours?+

Write the solution once from scratch. Precompute digit sums, bucket values, and run the rolling prefix-sum DP. Test on [1], [1,1], and [31,1] to confirm 4, 10, and 0. Then practice modulo handling so you don't lose points on overflow or negative values.

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

OA at Microsoft?
Invisible during screen share
Get it