Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Sum of Digits Coding

EasyPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Sum of Digits Coding. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

WebMD may need to derive a simple numeric checksum from an integer, such as a compact value associated with a health content identifier. Given an integer, return the sum of the digits in its absolute value.

Do not convert the number to a string. Use arithmetic operations to extract each digit.

Formal Specification

Implement sum_digits(number), where number is an integer. Return an integer equal to the sum of all decimal digits in abs(number). For example, the digit sum of -508 is 5 + 0 + 8 = 13.

A value of 0 has a digit sum of 0.

Constraints

  • number is an integer
  • -10^18 <= number <= 10^18
  • The input is not provided as a string
  • String conversion must not be used to calculate the result

Function Signature

def sum_digits(number):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output