Java Array Questions- Real Time Examples-Part 2
Introduction:
If you are preparing for technical interviews, mastering Java Array Coding Interview Questions is essential. These Java Array Coding Interview Questions are widely asked to test your problem-solving ability, logical thinking, and understanding of array-based concepts.
In this guide, we cover advanced Java Array Coding Interview Questions with clear explanations, optimized logic, and clean Java implementations. Practicing these Java Array Coding Interview Questions will help you build confidence and perform better in coding interviews.
11. Rotate Array by K Steps
Description:
Rotate an array to the right by k steps. This problem is commonly used to test your understanding of array manipulation without using extra space.
Logic:
- First Reverse entire array
- Then Reverse first k elements
- Reverse remaining elements
Code
import java.util.Arrays;
public class RotateArray {
public static void reverse(int[] arr, int start, int end) {
while (start < end) {
int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}
}
public static void rotate(int[] arr, int k) {
int n = arr.length;
k = k % n;
reverse(arr, 0, n - 1);
reverse(arr, 0, k - 1);
reverse(arr, k, n - 1);
}
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5}; // Input
int k = 2;
rotate(arr, k);
System.out.println("Rotated Array: " + Arrays.toString(arr)); // Output
}
}
12. Merge Two Sorted Arrays
Description:
Merge two sorted arrays into a single sorted array. This is a fundamental problem that appears in sorting and divide-and-conquer algorithms.
Logic:
Use two pointers to compare elements and build the result array.
Code:
import java.util.Arrays;
public class MergeArrays {
public static int[] merge(int[] a, int[] b) {
int[] result = new int[a.length + b.length];
int i = 0, j = 0, k = 0;
while (i < a.length && j < b.length) {
if (a[i] < b[j]) {
result[k++] = a[i++];
} else {
result[k++] = b[j++];
}
}
while (i < a.length) {
result[k++] = a[i++];
} while (j < b.length) {
result[k++] = b[j++];
}
return result;
}
public static void main(String[] args) {
int[] a = {1, 3, 5}; // Input
int[] b = {2, 4, 6};
System.out.println("Merged Array: " + Arrays.toString(merge(a, b))); // Output
}
}
13. Find Intersection of Two Arrays
Description:
Find common elements between two arrays. This question tests your understanding of hashing and efficient lookup.
Logic:
Store elements of first array in a set and check elements of second array.
Code:
import java.util.HashSet;
import java.util.Set;
public class IntersectionArray {
public static Set<Integer> findIntersection(int[] a, int[] b) {
Set<Integer> set = new HashSet<>();
Set<Integer> result = new HashSet<>();
for (int num : a) {
set.add(num);
}
for (int num : b) {
if (set.contains(num)) {
result.add(num);
}
} return result;
}
public static void main(String[] args) {
int[] a = {1, 2, 3, 4}; // Input
int[] b = {3, 4, 5, 6};
System.out.println("Intersection: " + findIntersection(a, b)); // Output
}
}
14. Find Union of Two Arrays
Description:
Union means finding out the unique value. In this problem statement , we need to combine two arrays and return unique elements. This problem tests understanding of set operations.
Logic:
Use LinkedHashSet to store unique elements while maintaining order.
Code:
import java.util.LinkedHashSet;
import java.util.Set;
public class UnionArray {
public static Set<Integer> findUnion(int[] a, int[] b) {
Set<Integer> set = new LinkedHashSet<>();
for (int num : a) {
set.add(num);
}
for (int num : b) {
set.add(num);
}
return set;
}
public static void main(String[] args) {
int[] a = {1, 2, 3}; // Input
int[] b = {3, 4, 5};
System.out.println("Union: " + findUnion(a, b)); // Output
}
}
These Java Array Coding Interview Questions are commonly asked in interviews to evaluate how efficiently you can handle array manipulation and real-world problem-solving scenarios.
15. Frequency of Elements in Array
This is one of the most asked question in Java Array coding
Description:
Count how many times each element appears in the array. This is useful in data analysis and frequency-based problems.
Logic:
Use HashMap to store element count.
Code:
import java.util.HashMap;
import java.util.Map;
public class FrequencyCount {
public static Map<Integer, Integer> getFrequency(int[] arr) {
Map<Integer, Integer> map = new HashMap<>();
for (int num : arr) {
map.put(num, map.getOrDefault(num, 0) + 1);
}
return map;
}
public static void main(String[] args) {
int[] arr = {1, 2, 2, 3, 3, 3}; // Input
System.out.println("Frequency: " + getFrequency(arr)); // Output
}
}
16. Check if Array is Sorted
Description:
Check whether the array is sorted in ascending order.
Logic:
Compare each element with the next element.
Code:
public class CheckSorted {
public static boolean isSorted(int[] arr) {
for (int i = 0; i < arr.length - 1; i++) {
if (arr[i] > arr[i + 1]) {
return false;
}
}
return true;
}
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4}; // Input
System.out.println("Is Sorted: " + isSorted(arr)); // Output
}
}
17. Find Leaders in Array
Description:
An element is a leader if it is greater than all elements to its right.
Logic:
Traverse from right and track maximum.
Code:
public class LeadersArray {
public static void findLeaders(int[] arr) {
int maxFromRight = arr[arr.length - 1];
System.out.print("Leaders: " + maxFromRight + " ");
for (int i = arr.length - 2; i >= 0; i--) {
if (arr[i] > maxFromRight) {
maxFromRight = arr[i];
System.out.print(maxFromRight + " ");
}
}
}
public static void main(String[] args) {
int[] arr = {16, 17, 4, 3, 5, 2}; // Input
findLeaders(arr); // Output
}
}
18. Left Rotate Array by One
Description:
Without this Java Array question , we can’t say we have preapred the coding question properly.
Rotate array elements to the left by one position.
Logic:
Store first element and shift remaining elements.
Code:
import java.util.Arrays;
public class LeftRotate {
public static void rotateLeft(int[] arr) {
int first = arr[0];
for (int i = 0; i < arr.length - 1; i++) {
arr[i] = arr[i + 1];
}
arr[arr.length - 1] = first;
}
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5}; // Input
rotateLeft(arr);
System.out.println(Arrays.toString(arr)); // Output
}
}
19. Find Pair with Given Difference
Description:
Find if there exists a pair with a given difference.
Logic:
Use HashSet for quick lookup.
Code:
import java.util.HashSet;
import java.util.Set;
public class PairDifference {
public static boolean findPair(int[] arr, int diff) {
Set<Integer> set = new HashSet<>();
for (int num : arr) {
if (set.contains(num - diff) || set.contains(num + diff)) {
return true;
}
set.add(num);
}
return false;
}
public static void main(String[] args) {
int[] arr = {1, 5, 3, 4, 2}; // Input
int diff = 2;
System.out.println("Pair Exists: " + findPair(arr, diff)); // Output
}
}
20. Find Subarray with Given Sum
Description:
Find a continuous subarray whose sum equals a given value.
Logic:
Use sliding window technique.
Code:
public class SubarraySum {
public static void findSubarray(int[] arr, int target) {
int start = 0, sum = 0;
for (int end = 0; end < arr.length; end++) {
sum += arr[end];
while (sum > target) {
sum -= arr[start++];
} if (sum == target) {
System.out.println("Subarray found from index " + start + " to " + end);
return;
}
}
}
public static void main(String[] args) {
int[] arr = {1, 2, 3, 7, 5}; // Input
int target = 12;
findSubarray(arr, target); // Output
}
}
Conclusion
In this second part of Java Array Coding Interview Questions, we explored more advanced and practical problems that are frequently asked in technical interviews. These Java Array Coding Interview Questions focus on real-world scenarios such as array rotation, merging, frequency counting, and subarray problems, which are essential for building strong problem-solving skills.
Understanding the logic behind these Java Array Coding Interview Questions is more important than simply memorizing solutions. When you practice these problems consistently, you develop the ability to approach different variations with confidence and clarity during interviews.
To get the best results, try implementing each problem on your own, optimize your solutions, and analyze different approaches. Mastering these Java Array Coding Interview Questions will not only help you crack interviews but also strengthen your overall programming foundation.
External Resources for Practice
Here are some high-quality resources where you can practice and deepen your understanding of Java array coding interviews
Final Tip:
For Java Array coding ,
Don’t just read — code along.
Don’t just code — explain it out loud.
That’s how you crack interviews like a pro.
Have a look on Testng related Blog TestNG Automation Framework – Complete Architect Guide for Enterprise CI/CD & Parallel Execution
Have a look on Cucumber related Blog For a complete BDD implementation guide, read our Cucumber Automation Framework – Complete Beginner to Advanced Guide.
Have a look on API Authentication related Blog , read our The Ultimate API Authentication guide
Want to prepare for Playwright interview questions , read our Playwright-Interview-Questions-Guide