Find Minimum
Java coding interview problem for Array Coding: Find Minimum.
Finding the minimum element in an array is one of the most common beginner-level Java interview questions.
Although the problem looks simple, it helps you understand several important programming concepts:
- Array Traversal
- Comparison Operators
- Variables
- Loops
- Time Complexity
- Space Complexity
Many advanced interview questions are built upon this concept, including:
- Second Smallest Element
- Minimum Difference
- Minimum Product
- Minimum Cost Problems
- Stock Buy and Sell
- Dynamic Programming Problems
Understanding how to efficiently find the minimum element prepares you for solving more complex array problems.
What is the Minimum Element?
The minimum element is the smallest value present in an array.
Example
Array
[25, 18, 42, 9, 31]
Minimum
9
Another Example
[-8, -15, -3, -20]
Minimum
-20
Why is this Question Asked in Interviews?
Interviewers use this problem to evaluate your understanding of:
- Arrays
- Loop Traversal
- Conditional Statements
- Variables
- Algorithm Design
- Edge Cases
It is one of the first array questions asked before moving to more difficult problems.
Real-World Applications
Finding the minimum value is used in many real-world systems.
Banking
Daily Expenses
$120
$85
$210
$60
Minimum Expense
$60
Temperature Monitoring
Weekly Temperatures
30
27
34
25
29
Lowest Temperature
25°C
E-Commerce
Product Prices
$799
$699
$899
$649
Lowest Price
$649
Manufacturing
Machine Response Time
35 ms
42 ms
28 ms
40 ms
Fastest Response
28 ms
Sports Analytics
Running Times
13.4 sec
12.8 sec
13.1 sec
12.5 sec
Fastest Time
12.5 sec
Problem Statement
Given an integer array,
find the smallest element.
Example 1
Input
[20, 10, 40, 5]
Output
5
Example 2
Input
[99]
Output
99
Example 3
Input
[-8, -15, -2]
Output
-15
Example 4
Input
[7, 7, 7, 7]
Output
7
Understanding Minimum Search
Suppose we have
[25, 18, 42, 9, 31]
Start
Minimum = 25
Compare
18
↓
Smaller
Minimum = 18
Compare
42
↓
Greater
Ignore
Compare
9
↓
Smaller
Minimum = 9
Compare
31
↓
Greater
Ignore
Final Answer
9
Mathematical Concept
Assume
Minimum = First Element
For every element
If
Current < Minimum
↓
Update Minimum
Repeat until reaching the end of the array.
Array Traversal Visualization
Input
[14, 9, 18, 5, 21]
Minimum
↓
14
↓
Compare
9
↓
Update
Minimum = 9
↓
Compare
18
↓
Ignore
↓
Compare
5
↓
Update
Minimum = 5
↓
Compare
21
↓
Ignore
Result
5
Dry Run
Input
[18, 12, 25, 4, 30]
| Current Element | Minimum | Action |
|---|---|---|
| 18 | 18 | Initialize |
| 12 | 12 | Update |
| 25 | 12 | Ignore |
| 4 | 4 | Update |
| 30 | 4 | Ignore |
Output
4
Approach 1 — Linear Search (Recommended)
This is the most efficient and commonly expected interview solution.
The algorithm scans the array exactly once.
Whenever a smaller value is found,
the minimum value is updated.
Algorithm
- Initialize minimum as the first element.
- Traverse the array.
- Compare the current element with minimum.
- If smaller, update minimum.
- Return minimum.
Java Program
public class FindMinimumLinear {
public static int findMinimum(int[] numbers) {
if (numbers == null || numbers.length == 0) {
throw new IllegalArgumentException("Array must not be empty");
}
int minimum = numbers[0];
for (int i = 1; i < numbers.length; i++) {
if (numbers[i] < minimum) {
minimum = numbers[i];
}
}
return minimum;
}
public static void main(String[] args) {
int[] numbers = {25, 18, 42, 9, 31};
System.out.println("Minimum = " + findMinimum(numbers));
}
}
Output
Minimum = 9
Step-by-Step Code Explanation
Initialize minimum.
int minimum = numbers[0];
Traverse the array.
for(...)
Compare
numbers[i] < minimum
Update minimum.
minimum = numbers[i];
Return the answer.
return minimum;
Dry Run of Linear Search
Input
[11, 7, 15, 2, 9]
| Current | Minimum | Action |
|---|---|---|
| 11 | 11 | Initialize |
| 7 | 7 | Update |
| 15 | 7 | Ignore |
| 2 | 2 | Update |
| 9 | 2 | Ignore |
Output
2
Advantages
- Best interview solution.
- Easy to understand.
- Only one traversal.
- Constant extra space.
- Works for negative numbers.
Drawbacks
- Entire array must be scanned.
- Cannot stop early because a smaller value may appear later.
Approach 2 — Using Java Collections.min()
Java Collections Framework provides a built-in method for finding the minimum element.
This approach is useful when working with collections.
Algorithm
- Convert the array into a List.
- Call
Collections.min(). - Return the minimum value.
Java Program
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class FindMinimumCollections {
public static void main(String[] args) {
Integer[] numbers = {25, 18, 42, 9, 31};
List<Integer> list = Arrays.asList(numbers);
int minimum = Collections.min(list);
System.out.println("Minimum = " + minimum);
}
}
Output
Minimum = 9
Step-by-Step Code Explanation
Create an Integer array.
Integer[] numbers = {25, 18, 42, 9, 31};
Convert it into a List.
List<Integer> list = Arrays.asList(numbers);
Find the minimum.
Collections.min(list);
Print the answer.
System.out.println(minimum);
Time & Space Complexity
| Approach | Time | Extra Space |
|---|---|---|
| Linear Search | O(n) | O(1) |
| Collections.min() | O(n) | O(1)* |
Note:
Arrays.asList()creates a fixed-size list backed by the originalInteger[]array without copying elements. If you're starting from a primitiveint[], converting toInteger[]requires boxing and additional memory.
Comparison of Approaches
| Feature | Linear Search | Collections.min() |
|---|---|---|
| Interview Friendly | ⭐⭐⭐⭐⭐ | ⭐⭐⭐ |
| Performance | ⭐⭐⭐⭐⭐ | ⭐⭐⭐⭐⭐ |
| Easy to Understand | ⭐⭐⭐⭐⭐ | ⭐⭐⭐⭐ |
| Extra Library Required | ❌ | ✅ |
Works with Primitive int[] |
✅ | ❌ |
Advantages
- Both approaches execute in O(n) time.
- Linear Search requires no library methods.
Collections.min()produces concise and readable code.- Both correctly handle duplicate and negative values.
Drawbacks
- Linear Search requires manual implementation.
Collections.min()works only with collections.- Primitive arrays require boxing before using
Collections.min().
Approach 3 — Using Java Streams
Java 8 introduced the Stream API, which provides a concise way to process arrays and collections.
To find the minimum element, we can use:
Arrays.stream(array).min()
This approach traverses the array only once.
Algorithm
- Convert the array into a stream.
- Call the
min()terminal operation. - Retrieve the result using
getAsInt(). - Return the minimum element.
Java Program
import java.util.Arrays;
public class FindMinimumStreams {
public static int findMinimum(int[] numbers) {
return Arrays.stream(numbers)
.min()
.getAsInt();
}
public static void main(String[] args) {
int[] numbers = {25, 18, 42, 9, 31};
System.out.println("Minimum = " + findMinimum(numbers));
}
}
Output
Minimum = 9
Step-by-Step Code Explanation
Convert the array into a stream.
Arrays.stream(numbers)
Find the minimum value.
.min()
Retrieve the integer.
.getAsInt()
Return the answer.
Advantages
- Modern Java syntax.
- Concise implementation.
- Single traversal.
- Easy to read.
Drawbacks
- Slight Stream API overhead.
getAsInt()throws an exception for an empty array unless checked.- Less common than Linear Search in beginner interviews.
Approach 4 — Divide and Conquer
Instead of checking every element sequentially,
we divide the array into two halves.
Find the minimum in both halves.
Finally,
compare the two minimum values.
Return the smaller one.
This approach demonstrates recursion and divide-and-conquer algorithms.
Visualization
Input
[25, 18, 42, 9, 31, 14]
Split
[25 18 42 9 31 14]
/ \
[25 18 42] [9 31 14]
/ \ / \
Min=18 Min=9
\ /
Minimum = 9
Algorithm
- Divide the array into two halves.
- Find the minimum recursively.
- Compare the two minimum values.
- Return the smaller value.
Java Program
public class FindMinimumDivideConquer {
public static int findMinimum(int[] numbers, int left, int right) {
if (left == right) {
return numbers[left];
}
int mid = (left + right) / 2;
int leftMinimum = findMinimum(numbers, left, mid);
int rightMinimum = findMinimum(numbers, mid + 1, right);
return Math.min(leftMinimum, rightMinimum);
}
public static void main(String[] args) {
int[] numbers = {25, 18, 42, 9, 31, 14};
System.out.println(
findMinimum(numbers, 0, numbers.length - 1));
}
}
Output
9
Advantages
- Demonstrates recursion.
- Good foundation for divide-and-conquer algorithms.
- Useful in parallel processing.
Drawbacks
- More complex than Linear Search.
- Recursive overhead.
- Not recommended for this simple problem.
Approach 5 — Using Sorting
Another way to find the minimum element is to sort the array.
After sorting,
the first element becomes the minimum.
Although simple,
this is not recommended because sorting is slower than a single traversal.
Visualization
Input
[25, 18, 42, 9, 31]
Sort
[9, 18, 25, 31, 42]
First Element
9
Algorithm
- Sort the array.
- Return the first element.
Java Program
import java.util.Arrays;
public class FindMinimumSorting {
public static int findMinimum(int[] numbers) {
Arrays.sort(numbers);
return numbers[0];
}
public static void main(String[] args) {
int[] numbers = {25, 18, 42, 9, 31};
System.out.println(findMinimum(numbers));
}
}
Output
9
Advantages
- Easy to understand.
- Useful when the array is already being sorted.
Drawbacks
- Time complexity increases to O(n log n).
- Modifies the original array.
- Much slower than Linear Search.
Handling Negative Numbers
Many beginners incorrectly initialize the minimum as
int minimum = 0;
Example
[-8, -15, -2]
Wrong Result
0
Correct Result
-15
Always initialize using the first element.
int minimum = numbers[0];
Integer Overflow Considerations
Finding the minimum only performs comparisons.
No arithmetic operations are involved.
Therefore,
integer overflow is generally not an issue.
Example
Integer.MIN_VALUE
can safely be compared.
if (numbers[i] < minimum)
Overflow becomes relevant only if arithmetic is later performed using the minimum value.
Edge Cases
| Input | Output |
|---|---|
[5] |
5 |
[-5] |
-5 |
[-10,-5,-20] |
-20 |
[7,7,7] |
7 |
[Integer.MIN_VALUE] |
Integer.MIN_VALUE |
[Integer.MAX_VALUE] |
Integer.MAX_VALUE |
Time & Space Complexity
| Approach | Time | Extra Space |
|---|---|---|
| Linear Search | O(n) | O(1) |
| Collections.min() | O(n) | O(1)* |
| Java Streams | O(n) | O(1) |
| Divide & Conquer | O(n) | O(log n) |
| Sorting | O(n log n) | O(log n)** |
*For an existing
Integer[]wrapped byArrays.asList(). Converting from a primitiveint[]requires boxing.
**
Arrays.sort(int[])uses Dual-Pivot Quicksort for primitive arrays, requiring approximately O(log n) stack space.
Comparison of All Approaches
| Approach | Interview Friendly | Performance | Extra Space | Best Use Case |
|---|---|---|---|---|
| Linear Search | ⭐⭐⭐⭐⭐ | ⭐⭐⭐⭐⭐ | O(1) | Recommended solution |
| Collections.min() | ⭐⭐⭐ | ⭐⭐⭐⭐⭐ | O(1)* | Collections |
| Java Streams | ⭐⭐⭐⭐ | ⭐⭐⭐⭐ | O(1) | Modern Java |
| Divide & Conquer | ⭐⭐⭐⭐ | ⭐⭐⭐ | O(log n) | Recursion |
| Sorting | ⭐⭐⭐ | ⭐⭐ | O(log n) | Array already being sorted |
Common Interview Mistakes
Mistake 1
Initializing minimum as zero.
Wrong
int minimum = 0;
Correct
int minimum = numbers[0];
Mistake 2
Ignoring empty arrays.
Always validate input.
if (numbers == null || numbers.length == 0) {
throw new IllegalArgumentException("Array must not be empty");
}
Mistake 3
Sorting only to find the minimum.
Sorting increases time complexity unnecessarily.
Mistake 4
Using Collections.min() directly on int[].
Primitive arrays are not collections.
Mistake 5
Accidentally modifying the original array.
Arrays.sort() changes the array in place.
Interview Follow-up Questions
Q1. Can you find both minimum and maximum in one traversal?
Q2. Can you find the second smallest element?
Q3. How would you solve this recursively?
Q4. Which approach is the fastest?
Q5. Why isn't sorting recommended?
Q6. How would you handle empty arrays?
Q7. Can Java Streams replace Linear Search?
Q8. What happens if duplicate minimum values exist?
Q9. How would this change for floating-point numbers?
Q10. How would you process billions of numbers stored in multiple files?
Related Problems
- Find Maximum Element
- Find Minimum and Maximum Together
- Second Smallest Element
- Second Largest Element
- Maximum Difference
- Minimum Difference
- Peak Element
- Kth Smallest Element
- Kadane's Algorithm
Key Takeaways
- Finding the minimum element is a fundamental array interview problem.
- Linear Search is the simplest and most efficient interview solution.
- Java Streams provide a concise Java 8+ alternative.
- Divide and Conquer demonstrates recursion and algorithmic thinking.
- Sorting works but is inefficient for this problem.
- Always initialize the minimum using the first element, not zero.
Frequently Asked Interview Questions
Q1. Which solution is best for interviews?
Linear Search is the recommended solution because it runs in O(n) time using O(1) extra space.
Q2. Why shouldn't we sort the array?
Sorting requires O(n log n) time, whereas Linear Search only needs O(n).
Q3. Why initialize with the first element?
It correctly handles arrays containing only negative values.
Q4. Can Streams replace Linear Search?
Yes.
Arrays.stream(array).min() internally scans the array once and provides clean Java 8+ code.
Q5. How do you handle empty arrays?
Always validate the input.
if (numbers == null || numbers.length == 0) {
throw new IllegalArgumentException("Array must not be empty");
}
Interview Tip
If an interviewer asks:
"Find the minimum element in an array."
Start with the Linear Search solution because it is the optimal approach.
Then discuss alternative implementations:
- Linear Search (Recommended)
- Java Streams (
Arrays.stream().min()) Collections.min()for collections- Divide and Conquer (Recursive approach)
- Sorting (Explain why it is less efficient)
Finally, discuss important edge cases:
- Empty arrays
- Negative numbers
- Duplicate minimum values
- Single-element arrays
Explaining both the optimal solution and the trade-offs between different approaches demonstrates strong problem-solving skills and interview readiness.