FastPrepAdd Digits Until One Remains

Add Digits Until One Remains

Adobe logoAdobe● EasyFULLTIMEPHONE SCREEN

Updated

Problem statement

Given a non-negative integer n, add up its decimal digits. If the result has more than one digit, add up its digits again. Repeat until one digit is left, and return that digit.

If n already has one digit (including 0), return it unchanged.

Function

addDigits(n: int) → int

Examples

Example 1

n = 4728return = 3

4 + 7 + 2 + 8 = 21, which has two digits, so add again: 2 + 1 = 3.

Example 2

n = 81return = 9

8 + 1 = 9, already one digit. A multiple of 9 ends at 9, not at 0.

Example 3

n = 5return = 5

5 already has one digit, so it is the answer.

Constraints

  • 0 <= n <= 2^31 - 1

More Adobe Phone Screen questions

Similar questions at other companies

Interview reports that mention this question

Adobe interview experiences

All Adobe interview questionsSee Adobe hiring insights
Saved
class Solution {
    public int addDigits(int n) {
        // Write your code here.
    }
}
Checking account…