Largest Number With Digit Sum
Reported by candidates from Amazon's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Amazon reported this one in August 2025, and the opening angle is a trap: you don't need a fancy data structure. The solution hinges on two integer counts, not a heap or a map. You're given digits x and y and a target sum n, and you build the biggest number from only those digits. It's tagged math for a reason. If you've got an Amazon OA coming up, the real work is picking the right counts, then printing the string. StealthCoder is the invisible safety net if your mind goes blank mid-assessment, but the idea fits in your head.
The problem
You are given three integers x, y, and n, where x and y are digits from 1 to 9. Construct the largest possible decimal number whose digits are only x and y, and whose digit sum is exactly n. It is guaranteed that at least one valid number exists. Function largestNumberWithDigitSum(x: int, y: int, n: int) → String Examples Example 1 x = 3 y = 4 n = 13 return = "4333" Some valid numbers are 3334, 3343, 3433, and 4333. The largest is 4333. Constraints 1 <= x, y <= 9 1 <= n <= 10^6 At least one valid answer exists.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Bigger number means more digits first, then bigger digits earlier. So maximize the total digit count by using as many copies of the smaller digit as possible. Let a be the count of the smaller digit s and b the count of the larger digit l, with a*s + b*l = n. Loop b from 0 up to n/l, check that (n - b*l) is divisible by s, and compute a. The length is a+b. Pick the largest length, and on ties pick the largest b. Then output all l's first, then all s's. The pitfall is greedy on the big digit first, which shrinks the length and gives the wrong answer. Another pitfall is building strings by repeated concatenation with n up to 10^6. Use one repeat or a join. If x equals y, handle it as a single digit. StealthCoder is your hedge on the live OA if the count logic slips under pressure.
If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.
You can drill Largest Number With 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. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Amazon's OA.
Amazon 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.
Largest Number With Digit Sum FAQ
What's the trick in Largest Number With Digit Sum?+
Length wins first. A longer number always beats a shorter one, so use as many of the smaller digit as you can. Then, among equal lengths, put the larger digit in front. Enumerate how many larger digits you use, check divisibility, and keep the best combination.
How hard is this Amazon OA question really?+
Easy to medium. There's no advanced structure, just careful arithmetic. The failure mode is greedy thinking that picks big digits first and loses length. Once you see the length-first rule, it's about fifteen lines of code.
What's the time complexity I should aim for?+
O(n) at worst. You loop over the count of the larger digit, which is at most n divided by that digit, and each check is constant time. Building the final string is O(n) too. With n up to 10^6, that's fine.
What edge cases break solutions?+
When x equals y, there's only one digit, so just repeat it n/x times. Also watch for the larger digit count being zero. Don't concatenate in a loop, since a million appends in some languages gets slow. Build with a repeat or join.
How do I prepare for this in 48 hours?+
Write this one from scratch twice. Then do a few problems where you choose counts of two items to hit a target sum. Practice reasoning about why length beats digit value. That reasoning is what the Amazon OA is testing, not syntax.