Find the Smallest Digit in a Number
Find the minimum digit of an integer using arithmetic only.
Peel off each digit with number % 10 and keep a running minimum, updating it whenever the current digit is smaller. Drop the digit with number / 10 and repeat until the number is 0. Start the minimum at 9 so the first digit always replaces it.
Problem Statement
Given an integer, return its smallest digit. For example the digits of 5824
are 5, 8, 2, 4, and the smallest is 2.
Scan the digits of the absolute value so negatives behave like positives, and
remember that any 0 digit makes the answer 0. Do not convert the number to
a String.
Input: A single integer n.
Output: A single digit (0-9) — the smallest digit of n.
Examples
Input: 5824
Output: 2Among 5, 8, 2, 4 the smallest digit is 2.
Input: 908
Output: 0Among 9, 0, 8 the smallest digit is 0.
Constraints
-2,147,483,648 <= n <= 2,147,483,647No String conversion
Think Before You Code
Reveal the questions to ask yourself first
- What starting value for a running minimum guarantees the first digit replaces it?
- Why is starting a minimum at 0 a mistake here?
- How do you extract and drop each digit?
- What happens when the number contains a 0 digit?
Hints
Open them one at a time — try after each before revealing the next.
Hint 1
Hint 2
Hint 3
Approach
Reveal the step-by-step approach
Track the minimum digit while walking the number.
- Take the absolute value of
n. - If it is 0, its only digit is 0, so return 0.
- Set
min = 9(the largest digit value, so any digit can beat it). - While the value is not 0:
digit = value % 10— the current last digit.- If
digit < min, setmin = digit. value = value / 10— drop the digit.
- Return
min.
Dry Run
Walk through the example step by step
Finding the smallest digit of n = 5824:
step | value | digit = value % 10 | min
-----+-------+--------------------+----
start | 9
1 | 5824 | 4 | 4
2 | 582 | 2 | 2
3 | 58 | 8 | 2
4 | 5 | 5 | 2
end | 0 | — | 2
Solution
Reveal the full Java solution
public class SmallestDigit {
public static int smallestDigit(int n) {
long value = Math.abs((long) n);
if (value == 0) {
return 0;
}
int min = 9;
while (value != 0) {
int digit = (int) (value % 10);
if (digit < min) {
min = digit;
}
value /= 10;
}
return min;
}
public static void main(String[] args) {
System.out.println(smallestDigit(5824)); // 2
System.out.println(smallestDigit(908)); // 0
}
}
Seeding min with 9 — the largest a single digit can be — guarantees the first
real digit overwrites it, and every later digit can only push the minimum lower.
The most common bug is starting min at 0: since no digit is negative, the
minimum would then stay 0 forever and every number would falsely report 0. The
absolute value keeps the sign from interfering.
O(d) where d is the number of digits (log10 n)Space: O(1)Common Mistakes
- Initialising min to 0, which locks the answer at 0 for every input.
- Forgetting that a 0 digit legitimately makes the smallest digit 0.
- Looping while (n > 0), which skips negatives.
Edge Cases to Test
- n = 0 returns 0.
- Any number with a 0 digit (like 908 or 5060) returns 0.
- Repeated digits like 3333 return 3.
Interview Follow-Ups
- How would you also report the position of the smallest digit?
- How would you find the smallest non-zero digit instead?
Practising for Java interviews?
CodeBegun's Java Full Stack with AI program builds this problem-solving muscle with mentor review and mock interviews.
Explore the Java Full Stack program →