Java Array Coding Interview Questions and Answers
Below is a collection of Java Array coding interview questions with solutions, examples, and outputs, ranging from beginner to advanced levels.
These questions are useful for Java developer and automation testing interviews (5–8 years of experience).
1. Basic Java Array Programs
1. Find the Largest Element in an Array
Input:
int[] arr = {10, 25, 5, 40, 15};
Solution:
public class Main {
public static void main(String[] args) {
int[] arr = {10, 25, 5, 40, 15};
int max = arr[0];
for (int i = 1; i < arr.length; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
System.out.println("Largest: " + max);
}
}
Output:
Largest: 40
2. Find the Smallest Element in an Array
int[] arr = {10, 25, 5, 40, 15};
int min = arr[0];
for (int i = 1; i < arr.length; i++) {
if (arr[i] < min) {
min = arr[i];
}
}
System.out.println("Smallest: " + min);
Output:
Smallest: 5
3. Find the Second-Largest Element
int[] arr = {10, 25, 5, 40, 15};
int first = Integer.MIN_VALUE;
int second = Integer.MIN_VALUE;
for (int num : arr) {
if (num > first) {
second = first;
first = num;
} else if (num > second && num != first) {
second = num;
}
}
System.out.println("Second Largest: " + second);
Output:
Second Largest: 25
4. Reverse an Array
import java.util.Arrays;
int[] arr = {10, 20, 30, 40, 50};
int left = 0;
int right = arr.length - 1;
while (left < right) {
int temp = arr[left];
arr[left] = arr[right];
arr[right] = temp;
left++;
right--;
}
System.out.println(Arrays.toString(arr));
Output:
[50, 40, 30, 20, 10]
5. Find the Sum of Array Elements
int[] arr = {10, 20, 30, 40, 50};
int sum = 0;
for (int num : arr) {
sum += num;
}
System.out.println("Sum: " + sum);
Output:
Sum: 150
6. Calculate the Average of Array Elements
int[] arr = {10, 20, 30, 40, 50};
int sum = 0;
for (int num : arr) {
sum += num;
}
double average = (double) sum / arr.length;
System.out.println("Average: " + average);
Output:
Average: 30.0
7. Check Whether an Array Is Sorted
int[] arr = {10, 20, 30, 40, 50};
boolean sorted = true;
for (int i = 0; i < arr.length - 1; i++) {
if (arr[i] > arr[i + 1]) {
sorted = false;
break;
}
}
System.out.println("Sorted: " + sorted);
Output:
Sorted: true
8. Search an Element in an Array
int[] arr = {10, 20, 30, 40, 50};
int search = 30;
boolean found = false;
for (int i = 0; i < arr.length; i++) {
if (arr[i] == search) {
System.out.println("Element found at index: " + i);
found = true;
break;
}
}
if (!found) {
System.out.println("Element not found");
}
Output:
Element found at index: 2
2. Intermediate Java Array Programs
9. Find Duplicate Elements in an Array
Input:
int[] arr = {10, 20, 30, 20, 40, 10, 50};
Solution:
import java.util.*;
public class Main {
public static void main(String[] args) {
int[] arr = {10, 20, 30, 20, 40, 10, 50};
Set<Integer> seen = new HashSet<>();
Set<Integer> duplicates = new LinkedHashSet<>();
for (int num : arr) {
if (!seen.add(num)) {
duplicates.add(num);
}
}
System.out.println(duplicates);
}
}
Output:
[20, 10]
10. Remove Duplicate Elements from an Array
import java.util.Arrays;
int[] arr = {10, 20, 30, 20, 40, 10, 50};
int[] result = Arrays.stream(arr)
.distinct()
.toArray();
System.out.println(Arrays.toString(result));
Output:
[10, 20, 30, 40, 50]
11. Count the Frequency of Each Element
import java.util.*;
int[] arr = {10, 20, 10, 30, 20, 10};
Map<Integer, Integer> map = new LinkedHashMap<>();
for (int num : arr) {
map.put(num, map.getOrDefault(num, 0) + 1);
}
System.out.println(map);
Output:
{10=3, 20=2, 30=1}
12. Find the Missing Number in an Array
Array contains numbers from 1 to 6, but one number is missing.
Input:
int[] arr = {1, 2, 4, 5, 6};
Solution:
int n = 6;
int expectedSum = n * (n + 1) / 2;
int actualSum = 0;
for (int num : arr) {
actualSum += num;
}
int missing = expectedSum - actualSum;
System.out.println("Missing number: " + missing);
Output:
Missing number: 3
13. Find Common Elements Between Two Arrays
import java.util.*;
int[] arr1 = {10, 20, 30, 40};
int[] arr2 = {30, 40, 50, 60};
Set<Integer> set = new HashSet<>();
for (int num : arr1) {
set.add(num);
}
for (int num : arr2) {
if (set.contains(num)) {
System.out.println(num);
}
}
Output:
30
40
14. Merge Two Arrays
import java.util.Arrays;
int[] arr1 = {10, 20, 30};
int[] arr2 = {40, 50, 60};
int[] result = new int[arr1.length + arr2.length];
System.arraycopy(arr1, 0, result, 0, arr1.length);
System.arraycopy(arr2, 0, result, arr1.length, arr2.length);
System.out.println(Arrays.toString(result));
Output:
[10, 20, 30, 40, 50, 60]
15. Move All Zeros to the End
Input:
int[] arr = {0, 10, 0, 20, 30, 0, 40};
Solution:
import java.util.Arrays;
int index = 0;
for (int num : arr) {
if (num != 0) {
arr[index++] = num;
}
}
while (index < arr.length) {
arr[index++] = 0;
}
System.out.println(Arrays.toString(arr));
Output:
[10, 20, 30, 40, 0, 0, 0]
16. Find the First Non-Repeating Element
import java.util.*;
int[] arr = {10, 20, 10, 30, 20, 40};
Map<Integer, Integer> map = new LinkedHashMap<>();
for (int num : arr) {
map.put(num, map.getOrDefault(num, 0) + 1);
}
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
if (entry.getValue() == 1) {
System.out.println("First non-repeating: " + entry.getKey());
break;
}
}
Output:
First non-repeating: 30
3. Advanced Java Array Coding Questions
17. Find Two Numbers Whose Sum Equals the Target
Input:
int[] arr = {2, 7, 11, 15};
int target = 9;
Solution:
import java.util.*;
int[] arr = {2, 7, 11, 15};
int target = 9;
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < arr.length; i++) {
int complement = target - arr[i];
if (map.containsKey(complement)) {
System.out.println(
map.get(complement) + ", " + i
);
break;
}
map.put(arr[i], i);
}
Output:
0, 1
18. Find the Maximum Sum of a Subarray (Kadane's Algorithm)
int[] arr = {-2, 1, -3, 4, -1, 2, 1, -5, 4};
int maxSum = arr[0];
int currentSum = arr[0];
for (int i = 1; i < arr.length; i++) {
currentSum = Math.max(arr[i], currentSum + arr[i]);
maxSum = Math.max(maxSum, currentSum);
}
System.out.println("Maximum sum: " + maxSum);
Output:
Maximum sum: 6
Explanation: The maximum-sum subarray is [4, -1, 2, 1].
19. Rotate an Array to the Right by K Positions
Input:
int[] arr = {1, 2, 3, 4, 5};
int k = 2;
Solution:
import java.util.Arrays;
int[] arr = {1, 2, 3, 4, 5};
int k = 2;
k = k % arr.length;
int[] result = new int[arr.length];
for (int i = 0; i < arr.length; i++) {
result[(i + k) % arr.length] = arr[i];
}
System.out.println(Arrays.toString(result));
Output:
[4, 5, 1, 2, 3]
20. Find the Product of All Elements Except Self
Input:
int[] arr = {1, 2, 3, 4};
Solution:
import java.util.Arrays;
int[] arr = {1, 2, 3, 4};
int n = arr.length;
int[] result = new int[n];
int prefix = 1;
for (int i = 0; i < n; i++) {
result[i] = prefix;
prefix *= arr[i];
}
int suffix = 1;
for (int i = n - 1; i >= 0; i--) {
result[i] *= suffix;
suffix *= arr[i];
}
System.out.println(Arrays.toString(result));
Output:
[24, 12, 8, 6]
21. Find the Majority Element
A majority element appears more than n/2 times.
int[] arr = {2, 2, 1, 2, 3, 2, 2};
int candidate = 0;
int count = 0;
for (int num : arr) {
if (count == 0) {
candidate = num;
}
count += (num == candidate) ? 1 : -1;
}
// Verify candidate
count = 0;
for (int num : arr) {
if (num == candidate) {
count++;
}
}
if (count > arr.length / 2) {
System.out.println("Majority element: " + candidate);
} else {
System.out.println("No majority element");
}
Output:
Majority element: 2
22. Find the Longest Consecutive Sequence
import java.util.*;
int[] arr = {100, 4, 200, 1, 3, 2};
Set<Integer> set = new HashSet<>();
for (int num : arr) {
set.add(num);
}
int longest = 0;
for (int num : set) {
if (!set.contains(num - 1)) {
int current = num;
int count = 1;
while (set.contains(current + 1)) {
current++;
count++;
}
longest = Math.max(longest, count);
}
}
System.out.println("Longest sequence: " + longest);
Output:
Longest sequence: 4
The sequence is [1, 2, 3, 4].
4. Java 8 Stream API Array Coding Questions
These examples are particularly useful for Java 8 and later interviews.
23. Find the Maximum Element Using Stream API
import java.util.Arrays;
int[] arr = {10, 20, 50, 30, 40};
int max = Arrays.stream(arr)
.max()
.orElseThrow();
System.out.println(max);
Output:
50
24. Find the Second-Highest Element Using Streams
import java.util.*;
import java.util.stream.*;
int[] arr = {10, 20, 50, 30, 40};
int secondHighest = Arrays.stream(arr)
.distinct()
.boxed()
.sorted(Comparator.reverseOrder())
.skip(1)
.findFirst()
.orElseThrow();
System.out.println(secondHighest);
Output:
40
25. Get Even Numbers from an Array
import java.util.Arrays;
int[] arr = {1, 2, 3, 4, 5, 6, 7, 8};
int[] even = Arrays.stream(arr)
.filter(n -> n % 2 == 0)
.toArray();
System.out.println(Arrays.toString(even));
Output:
[2, 4, 6, 8]
26. Sort an Array Using Streams
import java.util.Arrays;
int[] arr = {50, 10, 40, 20, 30};
int[] sorted = Arrays.stream(arr)
.sorted()
.toArray();
System.out.println(Arrays.toString(sorted));
Output:
[10, 20, 30, 40, 50]
27. Find Duplicate Elements Using Streams
import java.util.*;
import java.util.stream.*;
int[] arr = {10, 20, 30, 20, 40, 10};
Set<Integer> seen = new HashSet<>();
List<Integer> duplicates = Arrays.stream(arr)
.filter(n -> !seen.add(n))
.distinct()
.boxed()
.toList();
System.out.println(duplicates);
Output:
[20, 10]
28. Convert an Array into a List
import java.util.*;
String[] arr = {"Java", "Python", "Playwright"};
List<String> list = Arrays.asList(arr);
System.out.println(list);
Output:
[Java, Python, Playwright]
5. Java Array Interview Questions – Quick Revision
Here are additional questions to practice.
| No. | Coding Question | Level |
|---|---|---|
| 29 | Find third-largest element | Intermediate |
| 30 | Find third-smallest element | Intermediate |
| 31 | Find pairs with a given sum | Intermediate |
| 32 | Find triplets with a given sum | Advanced |
| 33 | Find intersection of two arrays | Intermediate |
| 34 | Find union of two arrays | Intermediate |
| 35 | Sort array without using built-in sort | Intermediate |
| 36 | Find leaders in an array | Advanced |
| 37 | Find equilibrium index | Advanced |
| 38 | Find maximum difference between two elements | Intermediate |
| 39 | Find subarray with a given sum | Advanced |
| 40 | Rearrange positive and negative numbers | Advanced |
| 41 | Find the longest increasing subsequence | Advanced |
| 42 | Find the trapped rainwater | Advanced |
| 43 | Merge two sorted arrays | Intermediate |
| 44 | Find the kth-largest element | Advanced |
| 45 | Find the kth-smallest element | Advanced |
| 46 | Find the minimum number of jumps | Advanced |
| 47 | Find the maximum product subarray | Advanced |
| 48 | Find duplicate number without extra space | Advanced |
6. Important Array Time Complexity
| Operation / Algorithm | Time Complexity |
|---|---|
| Access element by index | O(1) |
| Linear search | O(n) |
| Binary search (sorted array) | O(log n) |
| Reverse array | O(n) |
| Find maximum/minimum | O(n) |
| Sorting | O(n log n) |
| HashSet duplicate detection | O(n) average |
| Two Sum using HashMap | O(n) average |
| Kadane's algorithm | O(n) |
| Rotate array using extra space | O(n) |
7. Practice Java Array Interview Questions
Practice the following questions to improve your Java programming and problem-solving skills.
- Find the third-largest element in an array – Intermediate
- Find all pairs whose sum is equal to a target – Intermediate
- Move all negative numbers to the left side – Intermediate
- Find the maximum product subarray – Advanced
- Find the longest subarray with a given sum – Advanced
- Merge two sorted arrays without extra space – Advanced
- Find the missing and repeating numbers – Advanced
- Find the trapped rainwater between buildings – Advanced
Conclusion
Java Array coding questions are an important part of Java developer interviews. Start with basic array operations such as searching, sorting, reversing, and finding minimum and maximum elements.
Next, practice intermediate problems involving HashMap, HashSet, duplicate elements, missing numbers, and array rotation. Finally, focus on advanced algorithms such as Kadane's algorithm, Two Sum, majority element, and longest consecutive sequence.
Understanding the logic, time complexity, and space complexity of each solution will help you perform better in Java coding interviews.

No comments:
Post a Comment