Count Digits and Reverse Number
Digit Traversal (Modulo/Division)Solved Nov 10, 2025
Go step by step: try it yourself before the video and the visual. Progress saves on this device.
0 / 110 pointsSolved (Easy) 0/20Reviews 0/50Mastered 0/25Practice steps 0/15
Step 1
Know these before you start
Practise each one here, or tick what you already know
Watch the concept video if the pattern is new to you.
Step 2
Try it on paper
5–10 minutes, no code yet
ProblemProblem statementThe task is to master the fundamental technique of processing a number digit by digit:ReadHide
Problem Description
The task is to master the fundamental technique of processing a number digit by digit:
- Count Digits: Given an integer
N, count how many digits it has. - Reverse a Number: Given an integer
N, return the number formed by reversing its digits.
Code it here
Tab / Shift+Tab indent · ⌘/Ctrl+/ comments · ⌘/Ctrl+Enter runs · Esc leaves the editor
Step 6
Get it accepted
All test cases pass
Step 7
Review it so it sticks
Re-solve from memory after
The step that matters most: without reviews most of a solved problem fades within weeks. Three short re-solves, each just as you start to forget, are what make it yours.
Your notes
Three lines in your own words. They come back with this problem's reviews, so you can check yourself after recalling the solution.
Solve it yourself before you look
Reading the solution first feels like progress, but it makes the next similar problem — and the interview version — much harder, because you skipped the part where you figure it out. Give it an honest 20–30 minutes. Stuck? Re-read the pattern, watch the concept video, or try the brute force first.
Hidden: approach · solution code · optimal solution
Approach
Use a while loop that continues while N > 0. Inside the loop, extract the last digit (N % 10) and update N (N /= 10).
Approaches
1. Modulo and Division (Optimal) - O(log_10 N) ⭐
Idea: The time complexity is logarithmic because we reduce the number of digits by a factor of 10 in each constant-time iteration.
Reverse Number: Build the reversed number: reversed = reversed * 10 + last_digit.
Solution code
import sys
def solve_digit_manipulation():
try:
N = int(sys.stdin.readline().strip())
except:
return
original_n = N
# Handle negative numbers by converting to positive for digit logic
num = abs(N)
# --- 1. Count Digits ---
temp_count = num
count = 0
while temp_count > 0:
temp_count //= 10
count += 1
print(f"Count: {count}")
# --- 2. Reverse Number ---
temp_reverse = num
reversed_num = 0
while temp_reverse > 0:
last_digit = temp_reverse % 10
reversed_num = reversed_num * 10 + last_digit
temp_reverse //= 10
# Apply original sign
if original_n < 0:
reversed_num *= -1
print(f"Reversed: {reversed_num}")
if __name__ == "__main__":
solve_digit_manipulation()Other ways to solve it
- Thought of another approach (slower, or cleverer)? Code it here: the same tests check any approach.
Revising this one?
- →Solve it from memory before peeking
- →Say the pattern out loud, not just the code
- →Explain it like you would to a friend
- →Try it again in a different language