This article includes different types of array interview questions with complete answers, coding problems, and scenario-based thinking.
Table of Contents
Basic Array Interview Questions
1. How would you explain arrays to an interviewer without giving a textbook definition?Answer:
An array is a data structure used to store multiple values of the same type in continuous memory locations. Arrays help access elements quickly using indexes and are commonly used when the amount of data is known in advance.
Example:
Each value can be accessed using its position.Marks = [75, 82, 90, 88]
2. Why are arrays preferred in searching and indexing operations?
Answer:
Arrays allow direct access using indexes. The system does not need to visit every element before reaching the required position.
Example:
// Java program to show searching in
// an array
public class ArrayAccess
{
public static void main(String args[])
{
int arr[] = {10,20,30,40,50};
System.out.println(
"Element at index 3 = "
+ arr[3]);
}
}
Output:
The element can be accessed directly, which improves performance.Element at index 3 = 40
3. In which situations should arrays be avoided?
Answer:
Arrays are not suitable when frequent insertion, deletion, or resizing is required because shifting elements increases execution time.
Example:
Insert:[10,20,30,40]
Result:25
Elements after index 1 must move.[10,20,25,30,40]
4. What problems appear when array size becomes very large?
Answer:
Large arrays may increase memory usage and reduce efficiency if unnecessary copies are created. Optimized approaches such as sliding windows and in place modification become important.
5. How would you decide whether sorting should be applied before solving an array problem?
Answer:
Sorting is useful when order helps simplify searching, duplicates, or comparison operations. However, sorting should be avoided if original positions matter.
Example:
Sorting helps in duplicate detection but may fail when first occurrence positions are required.
Intermediate Array Interview Questions
6. How would you find the first repeating element whose second occurrence appears earliest?Example:
Answer:Input:
[7,2,5,2,3,5]
Output:
2
You can store visited elements using hashing and return the first value that repeats. This approach reduces unnecessary comparisons.
// Java program to find first repeating element
// whose second occurrence appears earliest
import java.util.HashSet;
public class FirstRepeating
{
public static void main(String args[])
{
int arr[] = {7,2,5,2,3,5};
HashSetInteger set =
new HashSet();
for(int num : arr)
{
if(set.contains(num))
{
System.out.println(num);
break;
}
set.add(num);
}
}
}
Output:
2
7. An array contains values from 1 to n, but one number is missing and another appears twice. How would you solve it?
Example:
Answer:Input:
[1,2,2,4,5]
Output:
Missing = 3
Duplicate = 2
You can use frequency arrays, hashing, or mathematical methods. Hashing is usually easier during interviews.
// Java program to find the missing number
// and duplicate number
public class MissingDuplicate
{
public static void main(String args[])
{
int arr[] = {1,2,2,4,5};
int freq[] =
new int[arr.length+1];
for(int num : arr)
{
freq[num]++;
}
int duplicate = -1;
int missing = -1;
for(int i=1;ifreq.length;i++)
{
if(freq[i] == 0)
{
missing = i;
}
if(freq[i] == 2)
{
duplicate = i;
}
}
System.out.println("Missing = "+ missing);
System.out.println("Duplicate = "+ duplicate);
}
}
Output:
8. How would you arrange positive and negative numbers alternately?Missing = 3
Duplicate = 2
Example:
Answer:Input:
[-4,7,-2,8,-1,5]
Output:
[7,-4,8,-2,5,-1]
Positive and negative values can be separated and merged alternatively. Extra care is needed when counts are unequal.
9. How would you find the longest consecutive sequence without sorting?
Example:
Answer:Input:
[100,4,200,1,3,2]
Output:
4
Hash sets help identify sequence starting points and reduce complexity to O(n).
// Java program to find longest consecutive
// sequence without sorting
import java.util.HashSet;
public class LongestSequence
{
public static void main(String args[])
{
int arr[] =
{100,4,200,1,3,2};
HashSetInteger 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);
}
}
Output:
10. How would you find the minimum swaps needed to bring smaller elements together?4
Example:
Answer:Input:
[2,7,9,5,8,7,4]
k = 5
Sliding window technique works effectively here. First count valid elements and minimize invalid values inside the window.
Coding Questions on Arrays
11. Write a program to move all zeros to the end without changing orderExample:
Answer:Input:
[0,1,0,3,12]
Output:
[1,3,12,0,0]
// Java program to move all zeros to the end
public class MoveZeros
{
public static void main(String args[])
{
int arr[] = {0,1,0,3,12};
int index = 0;
for(int i=0; iarr.length;i++)
{
if(arr[i]!=0)
{
int temp = arr[index];
arr[index] = arr[i];
arr[i]=temp;
index++;
}
}
for(int num:arr)
{
System.out.print(num+" ");
}
}
}
Output:
12. Write a program to find equilibrium index in an array.1 3 12 0 0
Example:
Answer:Input:
[2,3,-1,8,4]
Output:
3
// Java program to find equillibrium index
// in an array
public class Equilibrium
{
public static void main(String args[])
{
int arr[] = {2,3,-1,8,4};
int total = 0;
for(int num:arr)
{
total+=num;
}
int left = 0;
for(int i=0; iarr.length;i++)
{
total-=arr[i];
if(left==total)
{
System.out.println(i);
break;
}
left+=arr[i];
}
}
}
Output:
13. How would you explain to find the largest subarray having equal number of 0s and 1s.3
Example:
Answer:Input:
[1,0,1,1,0,0]
Output:
6
Convert every 0 into -1 and apply prefix sums. Equal prefix values indicate equal numbers of zeros and ones.
14. Write a program to find the maximum product subarray.
Example:
Answer:Input:
[2,3,-2,4]
Output:
6
// Java program to find maximum
// product subarray
public class MaxProduct
{
public static void main(String args[])
{
int arr[] = {2,3,-2,4};
int max=arr[0];
int min=arr[0];
int answer=arr[0];
for(int i=1;iarr.length;i++)
{
if(arr[i]0)
{
int temp=max;
max=min;
min=temp;
}
max=Math.max(arr[i],max*arr[i]);
min=Math.min(arr[i],min*arr[i]);
answer=Math.max(answer,max);
}
System.out.println(answer);
}
}
Output:
15. Write a program to find the first missing positive number.6
Example:
Answer:Input:
[3,4,-1,1]
Output:
2
// Java program to find first positive
// missing number
import java.util.HashSet;
public class MissingPositive
{
public static void main(String args[])
{
int arr[]={3,4,-1,1};
HashSetIntegerset=new HashSet();
for(int num:arr)
{
set.add(num);
}
int result=1;
while(set.contains(result))
{
result++;
}
System.out.println(result);
}
}
Output:
2
Advanced Array Questions for Placements
16. Write a program to calculate rainwater trapped between buildings.Example:
Answer:Input:
[3,0,2,0,4]
Output:
7
Rainwater trapping is an array problem where buildings of different heights are given, and you have to calculate how much water gets stored between them after rain. You can calculate left maximum and right maximum values for every position and then determine trapped water.
// Java program to calculate rainwater
// trapped between buildings
public class RainWaterTrapping
{
public static void main(String args[])
{
int arr[] = {3,0,2,0,4};
int n = arr.length;
int left[] = new int[n];
int right[] = new int[n];
left[0] = arr[0];
for(int i=1;in;i++)
{
left[i] = Math.max(left[i-1],arr[i]);
}
right[n-1] = arr[n-1];
for(int i=n-2;i=0;i--)
{
right[i] = Math.max(right[i+1],arr[i]);
}
int water = 0;
for(int i=0;in;i++)
{
water += Math.min(left[i],right[i])- arr[i];
}
System.out.println("Water Trapped = "+ water);
}
}
Output:
17. Write a program to solve product of array except self without division.Water Trapped = 7
Example:
Answer:Input:
[1,2,3,4]
Output:
[24,12,8,6]
Product of array except self means for every element in the array, find the product of all other elements except the current one, and division is not allowed. Prefix and suffix products are calculated separately and combined.
// Java program to find the product of
// array except self without division
public class ProductExceptSelf
{
public static void main(String args[])
{
int arr[] = {1,2,3,4};
int n = arr.length;
int left[] = new int[n];
int right[] = new int[n];
int result[] = new int[n];
left[0] = 1;
for(int i=1;in;i++)
{
left[i] = left[i-1] * arr[i-1];
}
right[n-1] = 1;
for(int i=n-2;i=0;i--)
{
right[i] = right[i+1] * arr[i+1];
}
for(int i=0;in;i++)
{
result[i] = left[i] * right[i];
}
for(int num : result)
{
System.out.print(num + " ");
}
}
}
Output:
18. Write a program to find the number of inversions in an array.24 12 8 6
Example:
Answer:Input:
[5,3,2,4,1]
Output:
8
Inversions in an array means a pair of elements is in the wrong order. Merge sort based counting gives O(n log n) complexity.
// Java program to find the number
// of inversions in an array
public class InversionCount
{
static int count = 0;
public static void main(String args[])
{
int arr[] = {5,3,2,4,1};
mergeSort(arr,0,arr.length-1);
System.out.println("Inversions = " + count);
}
static void mergeSort(int arr[],int left,
int right)
{
if(left right)
{
int mid = (left + right)/2;
mergeSort(arr, left, mid);
mergeSort(arr, mid+1, right);
merge(arr, left, mid, right);
}
}
static void merge(int arr[],int left,
int mid,int right)
{
int n1 = mid-left+1;
int n2 = right-mid;
int L[] = new int[n1];
int R[] = new int[n2];
for(int i=0;in1;i++)
{
L[i] = arr[left+i];
}
for(int j=0;jn2;j++)
{
R[j] = arr[mid+1+j];
}
int i=0;
int j=0;
int k=left;
while(in1 && jn2)
{
if(L[i] = R[j])
{
arr[k++] = L[i++];
}
else
{
arr[k++] = R[j++];
count += (n1-i);
}
}
while(in1)
{
arr[k++] = L[i++];
}
while(jn2)
{
arr[k++] = R[j++];
}
}
}
Output:
Inversions = 8
19. Write a program to find the median of two sorted arrays.
Example:
Answer:Input:
A=[1,3]
B=[2]
Output:
2
Binary search partition method gives optimized performance.
// Java program to find the median
// of two sorted arrays
public class MedianSortedArrays
{
public static void main(
String args[])
{
int arr1[] = {1,3};
int arr2[] = {2};
int merged[] = new int[arr1.length +
arr2.length];
int i=0;
int j=0;
int k=0;
while(iarr1.length &&
jarr2.length)
{
if(arr1[i] arr2[j])
{
merged[k++] =
arr1[i++];
}
else
{
merged[k++] = arr2[j++];
}
}
while(iarr1.length)
{
merged[k++] = arr1[i++];
}
while(jarr2.length)
{
merged[k++] = arr2[j++];
}
int n = merged.length;
double median;
if(n%2==0)
{
median = (merged[n/2] + merged[n/2-1])/2.0;
}
else
{
median = merged[n/2];
}
System.out.println(median);
}
}
Output:
20. Write a program to find the nearest greater element on right side.2.0
Example:
Answer:Input:
[4,5,2,10]
Output:
[5,10,10,-1]
Nearest greater element on the right side means for every element in the array, find the first greater element present on its right side. Stacks help solve this problem in O(n) complexity.
// Java program to find nearest
// greater element on right side
import java.util.Stack;
public class NextGreater
{
public static void main(String args[])
{
int arr[] = {4,5,2,10};
int result[] = new int[arr.length];
StackIntegerstack = new Stack();
for(int i=arr.length-1;i=0;i--)
{
while(!stack.isEmpty() &&
stack.peek() = arr[i])
{
stack.pop();
}
if(stack.isEmpty())
{
result[i] = -1;
}
else
{
result[i] = stack.peek();
}
stack.push(arr[i]);
}
for(int num:result)
{
System.out.print(num + " ");
}
}
}
Output:
5 10 10 -1
Scenario Based Placement Questions
21. If array size keeps increasing continuously, would you still choose arrays?Answer:
Arrays have fixed size, so dynamic structures such as ArrayList become better choices when data grows frequently.
22. If searching happens many times but insertion rarely happens, are arrays useful?
Answer:
Yes. Arrays perform well because searching becomes efficient, especially after sorting.
0 Comments