Add Digits

EasyMathmath

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

Given a non-negative integer num, replace it with the sum of its decimal digits, and keep doing so until only a single digit remains. Return that digit. For num = 0 the answer is 0. A loop works, but try to find an O(1) formula (the digital root).

Examples

Input: num = 38
Output: 2
3 + 8 = 11, then 1 + 1 = 2.
Input: num = 0
Output: 0
0 is already a single digit.

Constraints

  • 0 <= num <= 2^31 - 1
  • Digits are decimal (base 10).
  • The result is in the range [0, 9].

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More math problems