Wednesday, July 17, 2024

Top 30 Array Interview Questions and Answers for Programmers

The array data structure is one of the most important topics for programming interviews. It doesn't matter if you are going to Google, Microsoft, Amazon or investment banks like Goldman Sachs, Barclays, Citi or even service-based companies like IBM, Accenture, TCS, or Infosys, if you are going to interview for the developer position, you must prepare array very well. In fact, it is the second most topic after String and if you look closely, may of String coding problems can be solved by treating them like character array. The array is also ubiquitous, no matter which programming language you choose, you will find array there, it's available in C, C++, Java and even on Python, Perl, and Ruby.

In fact, it is one of the building blocks of programming tools along with arithmetic, relational, and bitwise operators. You learn about array in the very first week of your programming course and use that knowledge all your career.

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 multi-dimensional array in Java are nothing but an array of arrays, etc.

And, , here are important properties of array in Java which you must remember while solving these problems.

1. Array are fixed length, once created their length cannot be changed. 
2. You cannot store String in an int array in Java.
3. Array can be two or multi-dimensional in Java
4. In Java, 2 dimensional array is just array or array in Java
5. index based search in array can be done in O(1) time
6. insertion and deletion of array requires creating new array and copying elements hence not easy

Also, here is a nice cheat sheet to remember the time and space complexity of various array sorting algorithms which are needed to solve many of these problems and then calculating tie and space complexity of solution.

time and space complexity of various array sorting algorithms



30 Array Interview Questions for Programmers

Now, the question is how do you prepare for Array to do well on interviews? Everybody knows array, knows how to access elements from an array, knows how to sort array and search elements in an array using the binary search or a sequential search? will that be enough? 

Well, it depends upon where are you going for an interview, how much experience you have, and what is the job description.

If you are fresher with no experience, just out from college with a computer science degree and going to interview for Amazon or Google

If you are fresher with no experience, just out from college with a computer science degree and going to interview for Amazon or Google then you would be expected a sound knowledge of array related algorithms and you will find rather a difficult problem to solve.

On the other hand, if you are 4 to 5 years experienced guy, going for an Interview into banks like Goldman, Morgan Stanley or JP Morgan, you will hardly find difficult problems on an array like Amazon. 

Tech giants, FAANG/MAANG, or 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 a rough idea of what to expect but also helps to expand your knowledge on array.

30 Array Coding Problems for Programming Interviews

Ok, let's now start with my list of array interview questions, my solutions are in Java, but you are free to solve in any programming language of your choice.


1. How to find the missing number in integer array of 1 to 100? (solution)
This is one of the most simple array problems you will see, mostly asked in a telephonic round of Interview. You have given an integer array which contains numbers from 1 to 100 but one 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.

Array Interview Questions and Answer Java C++



2. How to find duplicate number on Integer array in Java? (solution)
An array contains n numbers ranging from 0 to n-2. There is exactly one number is repeated in the array. You need to write a program to find that duplicate number. For example, if an array with length 6 contains numbers {0, 3, 1, 2, 3}, then duplicated number is 3. 

Actually, this problem is very similar to previous one and you can apply the same trick of comparing actual sum of an array to expected sum of series to find out that duplicate. 

This is generally asked as follow-up question of previous problem.



3. How to check if array contains a number in Java? (solution)
Another interesting array problem, because array doesn't provide any built-in method to check if any number exists or not. This problem is essentially how to search an element in the array. There are two options sequential search or binary search

You should ask the interviewer about whether an array is sorted or not, if the array is sorted then you can use binary search to check if given number is present in an array or not. The complexity of binary search is O(logN). 

BTW, if interviewer says that array is not sorted then you can still sort and perform binary search otherwise you can use sequential search. Time complexity of sequential search in array is O(n).


4. How to find largest and smallest number in unsorted array? (solution)
This is a rather simple array interview question. You have given an unsorted integer array and you need to find the largest and smallest element in the array. 

Of course, you can sort the array and then pick the top and bottom element but that would cost you O(NLogN) because of sorting, getting element in array with index is O(1) operation.


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, it's neither too easy nor too difficult. You have given an integer array and a number, you need to write a program to find all elements in the array whose sum is equal to the given number. 

Remember, the array may contain both positive and negative numbers, so your solution should consider that. 

Don't forget to write unit test though, even if the interviewer is not asked for it, that would separate you from a lot of developers. Unit testing is always expected from a professional developer.


6.   How to find repeated numbers in an array if it contains multiple duplicates? (solution)
This is actually the follow-up question of problem 2, how to find a duplicate number on integer array. In that case, an array contains only one duplicate but what if it contains multiple duplicates? 

Suppose, an array contains n numbers ranging from 0 to n-1 and there are 5 duplicates on it, how do you find it? You can use the approach, we have learned in similar String based problem of finding repeated characters in given String.


7. Write a program to remove duplicates from array in Java? (solution)
This is another follow-up 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 an array. If substituting with another value is not an option then you need to create another array to mimic deletion.


8. How to sort an array in place using QuickSort algorithm? (solution)
You will often see sorting problems on array related questions because sorting mostly happen on array data structure. 

You need to write a program to implement in place quick sort algorithm in Java. You can implement either recursive or iterative quicksort, it's your choice but you cannot use additional buffer, array or list, you must sort array in place.


9.   Write a program to find intersection of two sorted arrays in Java? (solution)
Another interesting array interview question, where you need to treat the array as Set. Your task is to write a function in your favorite language e.g. Java, Python, C or C++ to return the intersection of two sorted arrays.

 For example, if the two sorted arrays as input are {21, 34, 41, 22, 35} and {61, 34, 45, 21, 11}, it should return an intersection array with numbers {34, 21}, For the sake of this problem you can assume that numbers in each integer array are unique.


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. This program is quite similar to finding the first non-repeated character on String because you can treat String as character array and use the same algorithm to solve this problem. 

Java Array Interview Question Answer


11. How to find kth smallest element in unsorted array? (solution)
You are given an unsorted array of numbers and k, you need to find the kth smallest number in the array. For example if given array is {1, 2, 3, 9, 4} and k=2 then you need to find the 2nd smallest number in the array, which is 2. 

One way to solve this problem is to sort the array in ascending order then pick the k-1th element, that would be your kth smallest number in array because array index starts at zero, but can you do better? 

Once you are able to solve this array coding question, you can solve many similar questions easily e.g. our next question.


12. How to find kth largest element in unsorted array? (solution)
This problem is exactly same as previous question with only difference being finding kth largest element instead of kth smallest number. For example if given array is {10, 20, 30, 50, 40} and k = 3 then your program should return 30 because 30 is the 3rd largest number in array. 

You can also solve this problem by sorting the array in decreasing order and picking k-1th element. I often seen this array question on Java interviews with 2 to 3 years experienced guys.



13 How to find common elements in three sorted array? (solution)
Now we are coming on territory of tough array questions. Given three arrays sorted in non-decreasing order, print all common elements in these arrays.

Examples:
input1 = {1, 5, 10, 20, 40, 80}
input2 = {6, 7, 20, 80, 100}
input3 = {3, 4, 15, 20, 30, 70, 80, 120}
Output: 20, 80


14. How find the first repeating element in an array of integers? (solution)
Given an array of integers, find the first repeating element in it. We need to find the element that occurs more than once and whose index of the first occurrence is smallest.

Examples:

Input:  input [] = {10, 5, 3, 4, 3, 5, 6}
Output: 5 [5 is the first element that repeats]


15. How to find first non-repeating element in array of integers? (solution)
This array interview question is exactly opposite of previous problem, In that you need to find first repeating element while in this you need to find first non-repeating element. I am sure you can use similar approach to solve this problem, just need to consider non repeating element though.


16. How to find top two numbers from an integer array? (solution)
This is another one of the easy array questions you will find on telephonic round of Interviews, but its also little bit tricky. You are asked to find top two numbers not just the top or highest numbers? Can you think of how you would do it without sorting? before looking at solution.


17. How to find the smallest positive integer value that cannot be represented as sum of any subset of a given array? (solution)
This is another tough array question you will see on Amazon, Microsoft or Google. You have given a sorted array (sorted in non-decreasing 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).

Examples:

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 vice-versa 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 sub-array 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 sub-array with 0 sum.

Examples:

Input: {4, 2, -3, 1, 6}
Output: true 

There is a sub-array with zero sum from index 1 to 3.

20. How to remove duplicates from array in place? (solution)
Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.

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],

Your function should return length = 2, and A is now [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.


22. How to merge sorted array? (solution)
Given two sorted integer arrays A and B, merge B into A as one sorted array. You may assume that A has enough space (size that is greater or equal to m + n) to hold additional elements from B. The number of elements initialized in A and B are m and n respectively. This is another intermediate array coding question, its not as simple as previous one but neither very difficult.


23. How to find sub array with maximum sum in an array of positive and negative number? (solution)
Another array coding question based upon sub-array. Here you have to find the contiguous sub-array within an array (containing at least one number) which has the largest sum.

For example, given the array [−2,1,−3,4,−1,2,1,−5,4],
the contiguous subarray [4,−1,2,1] has the largest sum = 6. 


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.

For example, given the array [2,3,-2,4],
the contiguous subarray [2,3] has the largest product = 6. 


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. 


26. How to find minimum value in a rotated sorted array? (solution)
This is another advanced level array coding question and you should only attempt this one, once you have solved the easier ones. Suppose a sorted array is rotated at some pivot unknown to you beforehand.

(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).

Find the minimum element.

You may assume no duplicate exists in the array. One follow-up question of this question is What if duplicates are allowed? Would this affect the run-time 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.

1. Returns the largest sum of contiguous integers in the array
Example: if the input is (-10, 2, 3, -2, 0, 5, -15), the largest sum is 8

2. Return the sum two largest integers in an array

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


28. How to reverse array in place in Java? (solution)
Now let's see one of the most frequently asked array interview question. You need to write a program which accepts an integer array and your program needs to reverse that array in place, which means you cannot use additional buffer or array, but one or two variables will be fine. 

Of course you cannot use any open source library or Java API method to directly solve this problem, you need to create your own logic. 

Here is one such logic to solve this problem :


Array Coding Interview Questions and Answers


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.


30. How to check if array contains a duplicate number? (answer)
This may look a repeated question because we have already done similar question, but in this question, most from Java interviews, you need to write a contains() like method from Collections, which returns true or false if you pass an element and it is repeated or not.


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. 


Related Data Structure and Algorithm Interview Questions from Javarevisited Blog
  • Top 15 Data Structure and Algorithm Interview Questions (see here)
  • 10 Matrix based problems for coding interviews (matrix problems)
  • Top 20 String coding interview questions (see here)
  • 133 core Java interview questions of last 5 years (see here)
  • 150 Programming Questions and Solution, the book (see here)
  • Top 30 linked list coding interview questions (see here)
  • 100+ System Design Interview Questions  (system design questions)
  • Top 50 Java Programs from Coding Interviews (see here)
  • Top 5 books on Programming/Coding Interviews (list)
  • 10 Dynamic Programming Questions from Coding Interviews (DP questions)
  • 25 Recursion based Interview questions (recursion questions)
  • 40 binary tree interview questions with answers (binary tree questions)
Thanks for reading this article so far. If you like this article then please share with your friends and colleagues. If you have any questions or doubt then please let us know and I'll try to find an answer for you.

44 comments :

Unknown said...

Please provide solutions to other problems.

Prashant Rohilla said...

Correct solution to problem #25

public class Practice {

public static void main(String[] args){
int[] a={32,65,4,3,1,2,3,4,5,6,7,8,9,10,11,56,1,3,14,15,34,4,5,6,7,8,9};
System.out.print(longest(a));
}
public static int longest(int[] b){
int temp=0;
int counter=0;
for(int i=1;itemp)
temp=counter;
continue;
}
counter=0;
}
}
if(temp>0){
return temp+1;
}
else{
return 0;
}
}
}

Unknown said...

input 1 2 3
4 5 6
7 8 9

output > 1 2 4 7 5 3 6 8 9 "print element of array diagonally in a sequence "

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?

Unknown 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?????

Unknown 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);

}
}

Valley Tech Guy said...

Pyhton Code for Q17>


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

Unknown said...

public static void firstRepeat(){
int[] a={1,7,8,6,3,8,4,2,5};
int count=0;
for(int i=0;i<a.length;i++){
for(int j=i+1;j<a.length;j++){
if(a[i]==a[j]){
System.out.println(a[i]);
count++;

}

}
if(count==1)
break;

}

}

public static void firstNotrepeat(){
int[] a= {1,2,3,7,4,6,1,5,4,3,2};
int count=0;
for(int i=0; i<a.length;i++){
for(int j=0; j<a.length;j++){
if(a[i]==a[j]){
count++;
}
}
if(count==1){

System.out.println(a[i]);
break;
}
count=0;
}
}

Unknown said...

How to find first non-repeating element in array of integers?

Can you provide the solution for this question?

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

public class ArrayIntersection {

public static void main(String[] args) {
int[] arr = {21, 34, 41, 22, 35};
int[] arr1 = {61, 34, 45, 21, 11};

intersection(arr, arr1);
}

private static Set intersection(int[] arr, int[] arr1) {
ArrayList arrList = new ArrayList();
Set setInt = new HashSet();

for (int i = 0; i < arr.length; i++) {
arrList.add(arr[i]);
}
for (int i = 0; i < arr1.length; i++) {
if (arrList.contains(arr1[i])) {
setInt.add(arr1[i]);
}
}
System.out.println(setInt.toString());
return setInt;
}

}

ngo said...

Pls let me know how to prepare job interview

javin paul said...

Hello @ngo, if you are preparing for programming job interview then you can take a look at job description first and then prepare accordingly, but you must be good at coding, data structure and algorithms and computer fundamentals.

Anonymous said...

Most of the Solution Links don't work , can you please fix it

Unknown said...

Hi
I am 2011 B.Tech in CSE from UPTU . Because of my medical and financial problem I have not worked in developer profile till now only worked in BPO.(4 companies)..Is there any chance for me to enter into software development field..last Nov.i left my job and preparing for fresher interview gave 8 interview till now and rejected in all ...is it over for me ?

Unknown said...

How to add given array with the adjacent array

Unknown said...

How to add array with the adjacent array???

Unknown said...

e.g. a[]={2,6,4,9,10};
then add adjacent elements of array and get new array

javin paul said...

@Muhammad, sorry for later reply but nothing is over for you. 8 interview is nothing, people give over 80 interview before they get selected. just work on your data structure, algorithm, problem solving and programming language skill and you will get a job very soon, if not get already.

javin paul said...

@Nitin, do you mean output = {2, 8, 10, 13, 19} then you can just go though a loop and add them, what is the problem.

Anonymous said...

sort array elements by frequency(occurrence),print an array in spiral way.

Unknown said...

plz give me program of this output
input :
a[]=1,2,3,4,5
b[]=6,7,8,9,10
output :
c[]=1,6,2,7,3,8,4,9,5,10

Unknown said...

Nitin Lohar
program for this question
input :
a[]=1,2,3,4,5
b[]=6,7,8,9,10
output :
c[]=1,6,2,7,3,8,4,9,5,10

solution:
public class Mergearray {
public static void main(String[] args) {
int arr1[]={1,2,3,4,5};
int arr2[]={6,7,8,9,10};
int tlen=arr1.length+arr2.length;
//System.out.println(tlen);
int result[]=new int[tlen];
int m=0;
int j=0;
int k=0;
for(int i=0;i<tlen;i++)
{
if(i%2==0)
{
//System.out.println(arr1[j]);
result[m++]=arr1[j];
j++;
}
else
{
//System.out.println(arr2[k]);
result[m++]=arr2[k];
k++;
}

}
for(int i=0;i<tlen;i++)
{
System.out.print(result[i]+" ");
}
}
}

Unknown said...

Qno.25

import java.util.Arrays;
public class LongestConsecutiveLength {

public static void main(String[] args)
{
int[] arr= {1,2,3,4,5,6,10,11,12,13,14,15,16,17,18,19,20};
int n=arr.length;
int count=0;
int[] arr1=new int[n];
int k=0;
for(int i=0; i=0; i--)
{
if(i==arr1.length-1)
System.out.print("Longest consecutive array length = "+arr1[i]);
}
}

}

arnold said...

QUESTION 28... Of course you CANNOT use any open source library or Java API method to directly solve this problem
Solution -> array.reverse();
looooooool
public class reverseInPlace {
public static void main(String[] args) {
int a[]={1,2,3,4,5,6,7},Imid,Jmid;
if(a.length%2!=0){
Imid=(a.length/2-1);
Jmid=Imid+2;
for(int i=0,j=a.length-1;i<=Imid && j>=Jmid;i++,j--){
int t=a[i];
a[i]=a[j];
a[j]=t;
}
}else{
Imid=(a.length/2);
Jmid=Imid;

for(int i=0,j=a.length-1;i<=Imid && j>=Jmid;i++,j--){
int t=a[i];
a[i]=a[j];
a[j]=t;
}
}
for(int i=0;i<a.length;i++){
System.out.print(a[i]+" ");
}
}
}

Unknown said...

Thanks

Unknown said...

How to find minimum value in a rotated sorted array?

Ans:
public class MinimumValueInRotatedSortedArray {
public static void main(String[] args) {
int[] a = { 4, 5 ,6, 7, 0, 1 ,2};
System.out.println("Min : "+findMin(a));
}

public static int findMin(int[] a) {

int min = a[a.length - 1];

for (int i = 0; i < a.length; i++) {
if (min > a[i]) {
min = a[i];
break;
}
}

return min;
}
}

Unknown said...

Q.28 ans:
public class ReverseArray {

public static int[] reverse(int[] array) {

int j = array.length-1;

for(int i=0; i<=j; i++)
{
int temp = array[i];
array[i] = array[j];
array[j] = temp;
j--;
}
System.out.println("Reverse Array : "+ Arrays.toString(array));
return array;
}

public static void main(String []args){
int[] data = {1,2,3,4,5,6,7,8,9};
reverse(data);

}

}

Taran Deep Singh said...

Easy...
I'm a 11th class student and I can solve all the programs.
Pls provide some though questions.

Unknown said...

These are job questions?
i am in 11th and i am able to solve these with ease

javin paul said...

Yes, mostly from interviews. If you can solve these questions then you are going in right direction. You algorithms and data structure, and programming skill is good.

Unknown said...

array1

lakshmimaran said...

(2015,2000,1990,1996,1995,1994,1993,1992,2001)
to find the who is 25 years old using array concept

Lakshmi said...

// Solution to 25th Question

public static void longestConsecutive(int a[]) {
Arrays.sort(a);
System.out.println(Arrays.toString(a));

Stack temp = new Stack<>();
Stack stack = new Stack<>();

for(int i=0; i< a.length-1; i++) {
temp.add(a[i]);
if(a[i+1] != a[i]+1) {
if(temp.size() == 1)
temp.pop();
else {
if(temp.size()> stack.size()) {
stack.clear();
stack.addAll(temp);
}
temp.clear();
}
}

}
if(temp.size()> stack.size()) {
stack.clear();
stack.addAll(temp);
}

if(stack.peek()+1 == a[a.length-1])
stack.add(a[a.length-1]);

System.out.println(stack);
}

Lakshmi said...

// Solution to 23rd Question
public static void findSubArray(int a[]) {
int thisSum = 0;
int highestSum = 0;
Queue q = new LinkedList<>();
int elements = 2;

for(int i=0; i highestSum) {
addElements(q, a, j, elements);
highestSum = thisSum;
}
}
}
System.out.println("The Highest Sum(SubArray) is: "+highestSum+" = "+q);
}

public static Queue addElements(Queue q, int a[], int index, int elements) {
q.clear();
for(int i= 0; i<elements; i++,index++) {
q.add(a[index]);
}
return q;
}

public static int getSumOfElements(int a[], int index, int elements) {
int sum = 0;
for(int i=0; i<elements; i++,index++)
sum = sum+a[index];
return sum;
}

Input: int a[] = { 1, 2 ,-3, 5, -7, 9, 10, -1, 4};

Output: The Highest Sum(SubArray) is: 22 = [9, 10, -1, 4]

javin paul said...

Hello Lakshmi, nice, keep it going.

Lakshmi said...

Thanks Javin. Your blog is interesting. Your explanations are clear and helpful.

I see some lines of code(forloops) are missing in 23rd solution. I am resending it.
//solution to 23rd question

public static void findSubArray(int a[]) {
int thisSum = 0;
int highestSum = 0;
Queue q = new LinkedList<>();
int elements = 2;

for(int i=0; i highestSum) {
addElements(q, a, j, elements);
highestSum = thisSum;
}
}
}
System.out.println("The Highest Sum(SubArray) is: "+highestSum+" = "+q);
}

public static Queue addElements(Queue q, int a[], int index, int elements) {
q.clear();
for(int i= 0; i<elements; i++,index++) {
q.add(a[index]);
}
return q;
}

public static int getSumOfElements(int a[], int index, int elements) {
int sum = 0;
for(int i=0; i<elements; i++,index++)
sum = sum+a[index];
return sum;
}

Input: int a[] = { 1, 2 ,-3, 5, -7, 9, 10, -1, 4};

Output: The Highest Sum(SubArray) is: 22 = [9, 10, -1, 4]

javin paul said...

Thanks Lakshmi, the > and < are HTML tags so blogger eat them if you haven't escape them

Thangavel said...

solution to 10 is as below, if the array is not sorted, first sort then iterate it.

public static void main(String[] args) {

int [] arr = {1,1,2,3,4,4,5,5};

for (int i = 0; i < arr.length;) {
if(arr[i] == arr[i+1]){
i = i + 2;
} else{
System.out.println("i = " + arr[i]);
break;
}
}
}

javin paul said...

Nice solution, impressive.

Unknown said...

//17* How to find the smallest positive integer value that cannot be represented
//as sum of any subset of a given array?
public void smallestNotFound(int ar[]) {
Arrays.sort(ar);
for(int i=0;i<ar.length-1;i++) {
if(ar[i]==ar[i+1] || ar[i]==ar[i+1]+1)continue;
else {
System.out.println(ar[i]+1);
break;
}
}
}

Fahad Nasrullah said...

Q23: Answer can also be [2 ,1, -3, 4, -1, 2, 1] So, sum = 6

Suraj Vishwakarma said...

//Removing Dublicate Element from Array
const array = [1,2,3,4,5,6,77,8,1,2,3,4]
const uniquearray = []
let obj = {}
for(let i = 0; i< array.length; i++){
obj[array[i]] = true
}

//METHOD ONE
//The Object.keys() method returns an array of a given
//object's own enumerable property names,
//iterated in the same order that a normal loop would.
console.log(Object.keys(obj));

//METHOD TWO
for(key in obj){
uniquearray.push(key)
}
console.log(uniquearray)

Post a Comment