Top 30 Array Interview Questions and Answers For Programmers
Top 30 Array Interview Questions and Answers For Programmers
Home core java spring hibernate collections multithreading design patterns interview questions coding data structure OOP books About Me
It's very important to keep some properties of array data structure in your mind e.g. array
index starts at 0, array stores its element at a contiguous memory location, C doesn't do array
bound checking but Java does, a multidimensional array in Java are nothing but an array of
arrays etc. I suggest reading one of the data structure and algorithm books before you go for
Interview.
SAMSUNG6.2KGFULLY
AUTOMATICTOP
Rs.15,790.00
(details+delivery)
Web Development
in Go - London,
three days, 1,344.
Practical training in web MAXIMAATTIVOANALOG
On the other hand if you are 4 to 5 years experienced guy, going for Interview into banks like
Goldman, Morgan Stanley or JP Morgan, you will hardly find difficult problems on array like
Amazon. Product companies like Google, Microsoft and Amazon focus more on data structure
and algorithm problems but banks mostly check your application development skills.
In this article, I am going to share with you some of the frequently asked array based coding
questions from programming interviews. They not only give you rough idea of what to expect
but also helps to expand your knowledge on array. If you need more questions, you can also
refer books likeCracking the Coding Interview, which contains almost 150 programming
questions and Solutions, it covers lot of topic but also has some decent questions on array.
Clickonvideotounmute
Recommended Reading
Javarevisited
Ok, let's now start with my list of array interview questions, my solutions are in Java, but you 29,624likes
are free to solve in any programming language of your choice.
LikePage LearnMore
1. How to find the missing number in integer array of 1 to 100? (solution) Translate this blog
This is one of the most simple array problems you will see, mostly asked in a telephonic round
SelectLanguage
of Interview. You have given an integer array which contains numbers from 1 to 100 but one
Poweredby Translate
number is missing, you need to write a Java program to find that missing number in an array.
You cannot use any open source library or Java API method which solves this problem. One
trick to solve this problem is to calculate sum of all numbers in the array and compare with
expected sum, the difference would be the missing number.
Java Tutorials
2. How to find duplicate number on Integer array in Java? (solution) Java JSON tutorial (8)
An array contains n numbers ranging from 0 to n2. There is exactly one number is repeated in Java multithreading Tutorials (37)
the array. You need to write a program to find that duplicate number. For example, if an array Java Programming Tutorials (27)
with length 6 contains numbers {0, 3, 1, 2, 3}, then duplicated number is 3. Actually this Java xml tutorial (11)
problem is very similar to previous one and you can apply the same trick of comparing actual
sum of array to expected sum of series to find out that duplicate. This is generally asked as
followup question of previous problem.
5. How to find all pairs on integer array whose sum is equal to given number? (solution)
This is an intermediate level of array coding question, its neither too easy nor too difficult.
You have given an integer array and a number, you need to write a program to find all
MAXIMAATTIVO
elements in array whose sum is equal to the given number. Remember, array may contain both ANALOGWHITE
positive and negative numbers, so your solution should consider that. Don't forget to write Rs.1,072.00
(details+delivery)
unit test though, even if interviewer is not asked for it, that would separate you from lot of
developers. Unit testing is always expected from a professional developer.
Followers
7. Write a program to remove duplicates from array in Java? (solution) Followers(4262)Next
This is another followup question from problem 2 and 6. You have given an array which
contains duplicates, could be one or more. You need to write a program to remove all
duplicates from array in Java. For example if given array is {1, 2, 1, 2, 3, 4, 5} then your
program should return an array which contains just {1, 2, 3, 4, 5}. This array question is also
comes at intermediate category because there is no way to delete an element from array. If
substituting with another value is not an option then you need to create another array to
Follow
mimic deletion.
Subscribe to Download the Ebook
21}, For the sake of this problem you can assume that numbers in each integer array are Emailaddress... Download
unique.
Adsby Google
QuestionandAnswer
JavaInterviews
10. There is an array with every element repeated twice except one. Find that element?
(solution)
This is an interesting array coding problem, just opposite of question related to finding
duplicates in array. Here you need to find the unique number which is not repeated twice. For
example if given array is {1, 1, 2, 2, 3, 4, 4, 5, 5} then your program should return 3. Also,
don't forget to write couple of unit test for your solution.
40} and k = 3 then your program should return 30 because 30 is the 3rd largest number in 2016 ( 166 )
array. You can also solve this problem by sorting the array in decreasing order and picking k 2015 ( 126 )
December ( 10 )
1th element. I often seen this array question on Java interviews with 2 to 3 years experienced
November ( 4 )
guys.
October ( 11 )
September ( 9 )
August ( 11 )
13 How to find common elements in three sorted array? (solution)
July ( 18 )
Now we are coming on territory of tough array questions. Given three arrays sorted in non
June ( 16 )
decreasing order, print all common elements in these arrays.
Difference between Dependency Injection and
Factor...
Examples: 2 Ways to check If String is Palindrome in Java?
R...
14. How find the first repeating element in an array of integers? (solution) 3 Ways to Find Duplicate Elements in an Array
Ja...
Given an array of integers, find the first repeating element in it. We need to find the element
Java Lock and Condition Example using Producer
that occurs more than once and whose index of the first occurrence is smallest. Con...
How to use Callable and Future in Java?
Examples: Example
How to Connect to MySQL database in Java with
Exam...
Input: input [] = {10, 5, 3, 4, 3, 5, 6}
How to converts Java Object to XML JAXB
Output: 5 [5 is the first element that repeats] Example
How to Read Write Excel file in Java POI
Example...
15. How to find first nonrepeating element in array of integers? (solution) Top 30 Array Interview Questions and Answers
for P...
This array interview question is exactly opposite of previous problem, In that you need to find
2 Websites to Learn Coding in Java Online for
first repeating element while in this you need to find first nonrepeating element. I am sure FREE...
you can use similar approach to solve this problem, just need to consider non repeating Google Protocol Buffers (ProtoBuf) Java
element though. Serializ...
Difference between Inheritance and
Composition in ...
16. How to find top two numbers from an integer array? (solution) May ( 5 )
This is another one of the easy array questions you will find on telephonic round of April ( 4 )
Interviews, but its also little bit tricky. You are asked to find top two numbers not just the top March ( 10 )
or highest numbers? Can you think of how you would do it without sorting? before looking at February ( 12 )
solution. January ( 16 )
2014 ( 100 )
2013 ( 127 )
17. How to find the smallest positive integer value that cannot be represented as sum of 2012 ( 214 )
any subset of a given array? (solution) 2011 ( 135 )
This is another tough array question you will see on Amazon, Microsoft or Google. You have 2010 ( 30 )
given a sorted array (sorted in nondecreasing order) of positive numbers, find the smallest
positive integer value that cannot be represented as sum of elements of any subset of given
set. What makes it more challenging is expected time complexity of O(n). Pages
Privacy Policy
Examples:
Copyright by Javin Paul 20102017. Powered by
Blogger.
Input: {1, 3, 6, 10, 11, 15};
Output: 2
18. How to rearrange array in alternating positive and negative number? (solution)
Given an array of positive and negative numbers, arrange them in an alternate fashion such
that every positive number is followed by negative and viceversa maintaining the order of
appearance.
Number of positive and negative numbers need not be equal. If there are more positive
numbers they appear at the end of the array. If there are more negative numbers, they too
appear in the end of the array. This is also a difficult array problem to solve and you need lot
of practice to solve this kind of problems in real interviews, especially when you see it first
time. If you have time constraint then always attempt these kind of questions once you are
done with easier ones.
Example:
Input: {1, 2, 3, 4, 1, 4}
Output: {4, 1, 1, 2, 3, 4}
Input: {5, 2, 5, 2, 4, 7, 1, 8, 0, 8}
output: {5, 5, 2, 2, 8, 4, 7, 1, 8, 0}
19. How to find if there is a sub array with sum equal to zero? (solution)
There is whole set of array related questions which are based upon subarray or only selective
elements of array e.g. from some range, this is one of such problem. Here you are given an
array of positive and negative numbers, find if there is a subarray with 0 sum.
Examples:
Input: {4, 2, 3, 1, 6}
Output: true
There is a subarray with zero sum from index 1 to 3.
Do not allocate extra space for another array, you must do this in place with constant
memory.
For example,
Given input array A = [1,1,2],
When you see a questions which asked you do to sorting or task in place, it means you cannot
use additional array or buffer, but using couple of variables is fine.
21. How to remove a given element from array in Java? (solution)
This is another array coding questions similar to previous one. Here you don't have to find and
remove duplicates but a given number. In this problem you are given an array and a value,
remove all instances of that value in place and return the new length. The order of elements
can be changed. It doesn't matter what you leave beyond the new length.
Adsby Google
ProgrammingQuestions
CProgramming
23. How to find sub array with maximum sum in an array of positive and negative number?
(solution)
Another array coding question based upon subarray. Here you have to find the contiguous
subarray within an array (containing at least one number) which has the largest sum.
24. How to find sub array with largest product in array of both positive and negative
number? (solution)
In this problem, your task is to write a program in Java or C++ to find the contiguous sub
array within an array (containing at least one number) which has the largest product.
25. Write a program to find length of longest consecutive sequence in array of integers?
(solution)
Given an unsorted array of integers, find the length of the longest consecutive elements
sequence.
For example,
Given [100, 4, 200, 1, 3, 2],
The longest consecutive elements sequence is [1, 2, 3, 4]. Return its length: 4.
Challenging part of this question is that your algorithm should run in O(n) complexity.
You may assume no duplicate exists in the array. One followup question of this question is
What if duplicates are allowed? Would this affect the runtime complexity? How and why?
27. Given an array of of size n and a number k, find all elements that appear more than
n/k times? (solution)
Another tough array based coding questions from Interviews. You are given an array of size n,
find all elements in array that appear more than n/k times. For example, if the input arrays is
{3, 1, 2, 2, 1, 2, 3, 3} and k is 4, then the output should be [2, 3]. Note that size of array is 8
(or n = 8), so we need to find all elements that appear more than 2 (or 8/4) times. There are
two elements that appear more than two times, 2 and 3.
3. Given an array of integers write a program that will determine if any two numbers add up
to a specified number N. Do this without using hash tables
29. Difference between array and linked list data structure? (answer)
This is a theoretical questions from phone interviews. There are several differences between
array and linked list e.g. array stores element in contiguous memory location while linked list
stores at random places, this means linked list better utilizes the places. Consequently, its
possible to have large linked list in limited memory environment compare to array of same
size. Advantage of using array is random access it provides if you know the index, while in
linked list you need to search an element by traversing which is O(n) operation.
That's all in this list of array interview questions for programmers. If you have solved all this
problems then you definitely have good preparation. You can now tackle any array based
coding problems, even though you will see it first time, mostly on coding interviews from top
software product companies like Amazon, Google, Microsoft and Facebook. If you have faced
any other interesting array based coding problems which is not included in this list then don't
forget to share with us, I will include in this list for everyone's benefit. You can also share any
array based coding problem which is not solved and you are looking for a decent solution.
Panasonic6.2kg Samsung23LSolo
Rs.6,299.00
Rs.15,131.00 (details+delivery)
(details+delivery)
VivoV5s(CrownGold,64GB)
Rs18,990flipkart.com
WhyAreBusinessesMovingtoCloudHosting?
Cloud4C
FinallyYouCanTrackYourCarUsingYourSmartphone
SmartDeviceTrends
[email protected],Bangalore
Puravankara
PhotographerFindsSomethingIncredibleInHitler'sForgottenBunker
TrendChaser
OneYearMarketResearch&DataAnalyticsCoursefromMICAOnlineClasses
MICA
GameofThrones:ActorsThenVsNow
Fropky
ApplyForRBLCreditCardAndGetInstantApprovalOnline
RBLBank
Recommendedby
16 comments :
Ankit Singh said...
Please provide solutions to other problems.
Anonymous said...
Few more questions to add :
How to find largest element in unsorted array?
How to find smallest element in unsorted array?
How to rotate an array in Java?
How to serach element in rotated array whose pivot is unknown?
How to merge two integer array?
How to calculate median of two arrays in Java?
G Prasath said...
Could you please provide some UnitTestCases for Problem 5? It will be of immense help to us...Thanks in advance.
Sardar said...
there was a question to print array elemnt on the basis of first it should print all prime number then odd number
then even number from the array?????
said...
public class Task22 {
public static int[] merge(int []a,int []b){
int[]answer =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])
answer[k++]=a[i++];
else
answer[k++]=b[j++];
}
while(i<a.length)
answer[k++]=a[i++];
while(j<b.length)
answer[k++]=b[j++];
for(int t =0;t<answer.length;t++){
System.out.print(" " + answer[t]);
}
return answer;
}
public static void main(String [] args){
int mas[]={12,2,3,4,5,6};
int mas1[]={11,8,9,10};
merge(mas,mas1);
}
}
def convert_binary(a):
ab=[]
if a==0:
ab=[0]
return ab
while a!=1:
b=a%2
ab.append(b)
a=a/2
ab.append(1)
ab=ab[::1]
return ab
def sum(a):
s=0
for i in a:
s=s+i
return s
a=[1,3,6,10,11,15]
print " The smallest possible integer is "
la=len(a)
out=[]
for i in range (2**la):
b=convert_binary(i)
if len(b)!=len(a):
diff=len(a)len(b)
while (diff!=0):
b.insert(0,0)
diff =1
b=b[::1]
#print b
c=[]
for i in range (len(b)):
if b[i]==1:
c.append(a[i])
#print c
s=sum(c)
if s not in out:
out.append(s)
i=1
while(1):
if i in out:
i +=1
else:
print i
break
}
if(count==1)
break;
System.out.println(a[i]);
break;
}
count=0;
}
}
Unknown said...
How to find first nonrepeating element in array of integers?
Thank you
GOPI said...
Thanks for the post Javin:) but i think some links for the solution are broken can u pls correct it:)
Anonymous said...
Answer to question 9 Array Intersection
intersection(arr, arr1);
}
ngo said...
Pls let me know how to prepare job interview
Anonymous said...
Most of the Solution Links don't work , can you please fix it
Post a Comment
Enteryourcomment...
Commentas: Selectprofile...
Publish
Preview