Count the digits of a number
Coding problemEasyAsked in: JPMorgan Chase, TCS
Given an integer n, return how many digits it has.
The minus sign of a negative number is not a digit, and 0 has one digit. Interviewers ask you to do this with arithmetic (dividing by ten), not by turning the number into text.
Examples
Example 1
Input: n = 12345
Output: 5
Example 2
Input: n = -907
Output: 3
The minus sign does not count.
Example 3
Input: n = 0
Output: 1
Constraints
- n is between -2,147,483,648 and 2,147,483,647
Run checks the 3 examples. Submit also runs the hidden tests: edge cases, large inputs and a time limit.
Run your code to see the results here. Ctrl or Cmd + Enter runs; add Shift to submit.
to answer and keep your progress. Reading is free for everyone.