Reported July 2026
Amazongreedy

Smallest Number With a Given Digit Sum

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

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

This Amazon OA, reported in July 2026, looks like a digit puzzle but it's really a greedy fill from the right. You're building the smallest number with a fixed length and a fixed digit sum. Smaller number means push the big digits to the end and keep the front as small as possible. If you've got the invite for the next day or two, this is a ten-line solution once you see it. If you blank on the idea under the clock, StealthCoder runs invisibly during the live assessment and hands you the approach as a safety net.

The problem

Given integers digitSum and numberOfDigits, construct the smallest non-negative decimal number that:
has exactly numberOfDigits digits, and
has digits whose sum is exactly digitSum.
Return the number as a string. The first digit cannot be zero unless numberOfDigits is 1. The input is guaranteed to admit at least one valid number.

Function
smallestNumberWithDigitSum(digitSum: int, numberOfDigits: int) → String

Examples
Example 1
digitSum = 20
numberOfDigits = 3
return = "299"
The smallest three-digit number with digit sum 20 is 299. Any smaller hundreds digit would leave more than 18 for the final two digits.
Example 2
digitSum = 1
numberOfDigits = 4
return = "1000"
The leading digit must be nonzero, so 1000 is the smallest four-digit number with digit sum 1.

Constraints
1 <= numberOfDigits.
0 <= digitSum <= 9 * numberOfDigits.
If numberOfDigits is greater than 1, then digitSum is positive.
The returned string has exactly numberOfDigits characters and no leading zero.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick: smallest number of fixed length means minimize the leftmost digits first. So fill from the last position backward, giving each position min(9, remaining sum). That packs the 9s at the end. Then the leftover goes to the front digits. Check example 1: sum 20, three digits. Last digit 9, remaining 11. Middle digit 9, remaining 2. First digit 2. Result 299. The pitfall is the leading zero. With digitSum 1 and four digits, the greedy gives 0001, which is invalid. Fix it by reserving 1 for the first digit when numberOfDigits is greater than 1. Subtract 1 from the sum, run the greedy fill, then add 1 back to the first digit. Example 2 gives 1000. Handle numberOfDigits equal to 1 separately, where 0 is allowed. It's O(n) time. If you freeze on the leading-zero edge case during the real OA, StealthCoder is the hedge that surfaces the fix.

StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.

If this hits your live OA

You can drill Smallest Number With a Given Digit Sum 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. If you're reading this with an OA window open, you're who this was built for.

Get StealthCoder

Related leaked OAs

⏵ The honest play

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

Amazon reuses patterns across OAs. If you're reading this with an OA window open, you're who this was built for. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Smallest Number With a Given Digit Sum FAQ

What's the trick in the Amazon smallest number with digit sum problem?+

Greedy from the right. Put min(9, remaining) in each position starting at the last digit, so the large digits sit at the end and the leading digits stay small. The only twist is the leading digit, which can't be zero when there's more than one digit.

How do I handle the leading zero?+

If numberOfDigits is greater than 1, subtract 1 from digitSum up front and reserve it for the first digit. Run the right-to-left greedy fill on the rest, then add the 1 back to the first position. For one digit, just return the sum as a string.

How hard is this one really?+

Easy to medium. There's no data structure and no search. The difficulty is seeing that the answer is built greedily and catching the leading-zero edge case. Most failures come from that edge, not the main loop.

Should I use brute force or DP?+

No. Enumerating numbers is hopeless for large digit counts, and DP is overkill. The greedy is provably optimal because pushing weight right always minimizes the left digits. A single pass with O(n) time is all you need.

How do I prep for this in 48 hours?+

Write the greedy by hand and test three cases: digitSum 20 with 3 digits, digitSum 1 with 4 digits, and digitSum 0 with 1 digit. Then try the maximum sum 9 times n. Also practice other construct-the-smallest-number problems so the right-to-left fill feels automatic.

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

OA at Amazon?
Invisible during screen share
Get it