Common Array Problems
Array problems are among the most frequently asked questions in Java interviews. Almost every problem is solved using array traversal patterns, condition checks, and basic logic. Mastering these problems builds a strong foundation for DSA, coding rounds, and real-world logic.
Why Array Problems Matter
- Frequently asked in interviews
- Test logic, loops, and condition handling
- Foundation for strings, collections, and algorithms
- Improve problem-solving confidence
1. Find the Largest Element in an Array
int max = arr[0];
for (int i = 1; i < arr.length; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
Key Concept: Linear traversal
Time Complexity: O(n)
2. Find the Smallest Element in an Array
int min = arr[0];
for (int i = 1; i < arr.length; i++) {
if (arr[i] < min) {
min = arr[i];
}
}
3. Find Sum of Array Elements
int sum = 0;
for (int value : arr) {
sum += value;
}
4. Find Average of Elements
double avg = (double) sum / arr.length;
Interview Tip: Type casting is important.
5. Search an Element (Linear Search)
int key = 20;
boolean found = false;
for (int value : arr) {
if (value == key) {
found = true;
break;
}
}
6. Count Even and Odd Numbers
int even = 0, odd = 0;
for (int value : arr) {
if (value % 2 == 0) {
even++;
} else {
odd++;
}
}
7. Reverse an Array
for (int i = arr.length - 1; i >= 0; i--) {
System.out.println(arr[i]);
}
Note: This prints reverse order; not in-place reversal.
8. Reverse an Array (In-Place)
int start = 0, end = arr.length - 1;
while (start < end) {
int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}
9. Copy One Array to Another
int[] copy = new int[arr.length];
for (int i = 0; i < arr.length; i++) {
copy[i] = arr[i];
}
10. Find Duplicate Elements
for (int i = 0; i < arr.length; i++) {
for (int j = i + 1; j < arr.length; j++) {
if (arr[i] == arr[j]) {
System.out.println(arr[i]);
}
}
}
Time Complexity: O(n²)
11. Remove Duplicate Elements (Basic Approach)
for (int i = 0; i < arr.length; i++) {
boolean duplicate = false;
for (int j = i + 1; j < arr.length; j++) {
if (arr[i] == arr[j]) {
duplicate = true;
break;
}
}
if (!duplicate) {
System.out.println(arr[i]);
}
}
12. Find Second Largest Element
int first = Integer.MIN_VALUE;
int second = Integer.MIN_VALUE;
for (int value : arr) {
if (value > first) {
second = first;
first = value;
} else if (value > second && value != first) {
second = value;
}
}
13. Check if Array Is Sorted
boolean sorted = true;
for (int i = 0; i < arr.length - 1; i++) {
if (arr[i] > arr[i + 1]) {
sorted = false;
break;
}
}
14. Merge Two Arrays
int[] merged = new int[a.length + b.length];
int index = 0;
for (int value : a) {
merged[index++] = value;
}
for (int value : b) {
merged[index++] = value;
}
15. Find Frequency of Each Element
for (int i = 0; i < arr.length; i++) {
int count = 1;
boolean visited = false;
for (int j = i - 1; j >= 0; j--) {
if (arr[i] == arr[j]) {
visited = true;
break;
}
}
if (!visited) {
for (int j = i + 1; j < arr.length; j++) {
if (arr[i] == arr[j]) {
count++;
}
}
System.out.println(arr[i] + " -> " + count);
}
}
Common Interview Patterns to Recognize
| Pattern | Used In |
|---|---|
| Single traversal | Max, min, sum |
| Two pointers | Reverse, comparison |
| Nested loops | Duplicates, frequency |
| Early exit (break) | Search problems |
| Index tracking | Second largest, sorted check |
Common Beginner Mistakes
- Off-by-one loop errors
- Not handling empty arrays
- Ignoring edge cases (single element)
- Incorrect variable initialization
- Missing break for optimization
Interview-Ready Summary
Short Answer
Common array problems involve searching, sorting checks, reversing, counting, and aggregation using traversal logic.
Detailed Answer
Array problems in Java typically rely on linear or nested traversal, condition checks, and basic logic. Understanding traversal patterns and edge cases allows efficient problem-solving without complex data structures.
Key Takeaway
Most array interview problems are variations of traversal and comparison. If you master loop control and array access, you can solve 90% of array-based questions confidently.