Reduce a Decimal Integer to One Digit

Quick Overview

Reduce a Decimal Integer to One Digit. Repeatedly replace num by the sum of its decimal digits until one digit remains. Return that final digit.

Reduce a Decimal Integer to One Digit

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

### Problem Repeatedly replace num by the sum of its decimal digits until one digit remains. Return that final digit. ```cpp int addDigits(int num); ``` ### Examples ```text num = 59 -> 5 num = 0 -> 0 ``` ### Constraints - 0 <= num <= 2147483647. ### Optional follow-up Can you solve it in O(1) time without loops or recursion?

Overview: Reduce a Decimal Integer to One Digit. Repeatedly replace num by the sum of its decimal digits until one digit remains. Return that final digit.

Google logo
Google
Date not reported
easySoftware EngineerOtherCoding & Algorithms
0
0

Problem

Repeatedly replace num by the sum of its decimal digits until one digit remains. Return that final digit.

int addDigits(int num);

Examples

num = 59 -> 5
num = 0 -> 0

Constraints

  • 0 <= num <= 2147483647.

Optional follow-up

Can you solve it in O(1) time without loops or recursion?