Add Digits
Reported by candidates from Arcesium's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The Arcesium OA reported in August 2023 hands you 38 and expects 2, via 38 -> 11 -> 2. That's the whole problem: keep summing digits until one is left. You can loop it in a few lines, and that's the trap, because there's a one-line answer hiding underneath. It's a math question dressed as simulation. If the formula slips your mind under the timer, StealthCoder is the invisible safety net that reads the problem on screen and gives you a working solution. Know the trick and you won't need it.
The problem
Given a non-negative integer num, repeatedly add all of its decimal digits until the result has only one digit. Return that single-digit result. Function addDigits(num: int) → int Examples Example 1 num = 38 return = 2 The process is 38 -> 3 + 8 = 11 -> 1 + 1 = 2. Example 2 num = 0 return = 0 The input already has one digit, so the result is 0. Example 3 num = 99999 return = 9 The digit sum is 45, and 4 + 5 = 9. Constraints 0 <= num <= 2^31 - 1
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is the digital root. A number and its digit sum have the same remainder mod 9, so the final single digit depends only on num mod 9. For num = 0 return 0. Otherwise return 1 + (num - 1) % 9. Check it: 38 gives 1 + 37 % 9 = 2. 99999 gives 1 + 99998 % 9 = 9. The common pitfall is writing num % 9 and returning 0 for multiples of 9, which breaks the 99999 case. Zero is the other edge case. The brute-force loop is also correct, since num fits in 2^31 - 1 and the digit sum shrinks fast. Write the loop first if you panic, then swap in the formula. If you blank on the mod 9 reasoning during the live OA, StealthCoder is the hedge that surfaces it. It runs O(1) time and O(1) space.
The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.
You can drill Add Digits 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 StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as add digits. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Arcesium's OA.
Arcesium 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.
Add Digits FAQ
What's the trick in Add Digits?+
Digital root. The result depends only on num mod 9. Return 0 when num is 0, otherwise 1 + (num - 1) % 9. It avoids loops entirely and runs in constant time and space. Memorize the formula and the reason: digit sums preserve remainders mod 9.
Is the loop solution acceptable for the Arcesium OA?+
Yes. Sum the digits of num, repeat until num is below 10. With num up to 2^31 - 1, it finishes in a handful of iterations. The formula is cleaner, but a correct loop passes the examples and the constraints fine.
Why does num % 9 fail for 99999?+
Because 99999 is a multiple of 9, so num % 9 gives 0, but the expected answer is 9. Shift the value with (num - 1) % 9 + 1 to map multiples of 9 to 9. Handle num = 0 separately so it returns 0.
What edge cases should I test?+
Test num = 0 (return 0), single digits like 7 (return 7), exact multiples of 9 like 9, 18 and 99999 (return 9), and the max value 2147483647. If all of those match your formula or loop, you're covered.
How do I prepare for this in 48 hours?+
Write both versions once from memory: the digit-sum loop and the mod 9 formula. Then trace 38 and 99999 by hand. That takes under 15 minutes. Spend the remaining time on other math and simulation problems, since the Arcesium OA likely has more than one question.