Monday, April 10, 2017

Java Array interview Question

Array Interview Question
Q 1 : Can you change size of array once created? 

Q2 : Can you store String in an array of Integer in Java? compile time error or runtime exception?
Q3 : What is difference between ArrayIndexOutfOBounds and ArrayStoreException? 

Q.4  How to search an array to check if an element exists there?
Q5.  How to copy array in Java?
Q6.  How to access elements of array in Java?

Q7.  What is difference between an array and a linked list?

Q8.  Can you make array volatile in Java?

Q9. Where does array stored in memory?
Q10. How to sort an array in Java?

Java coding interview  Question of Array
Q.1. How to find missing number in array of 1 to 100 in Java?
You have given an array of integer which contains numbers from 1 to 100, but exactly one number is missing, how do you find that number? 

Q2. Problem 2 : How do you find all pair whose sum is equal to given number from integer array in Java?
You have given an array of int primitives and a number, you need to find all pairs in array whose sum is equal to given number e.g. if array is {10, 20, 3,  4, 15} and given sum is 6 then your program should return {20, 4} and {10, 15}

Q3. Problem 3 : How do you remove duplicates from array in Java? 
You have given an array, which could be array of numbers or Strings or any objects. Some objects are added multiple times in array, you need to remove those elements from array. You don't know how many duplicates are there. You can use additional data structure and memory to solve this problem.
Q4. Problem 4 : How do you reverse an array in Java? 
Given an array of String or integer, how do you reverse array, so that first element becomes last and last element becomes first e.g. if given array is {12, 20, 3, 4} then your program should reverse it as {4, 3, 20, 12}.

Q5. How to find duplicates numbers in array of integers in Java?
You have given an array {1, 1, 2, 3, 3, 4,4,7}, write a program to return duplicate elements e.g. 1 and 3,4 because they have appeared more than once in array.

Q6. : How to find top two numbers from an integer array in Java? 

You have given an integer array e.g. { 3, 4,  5,  6 , 7}, you need to find top 2 numbers from this array e.g. your program should print 6 and 7.
Q7. What are the different ways of copying an array into another array?
Q8. What are jagged arrays in java? Give example?
Q9. How do you check the equality of two arrays in java? OR How do you compare the two arrays in java?
Q10.  How do you find the intersection  and union of two arrays in java?
Q11. What are the different ways to iterate over an array in java?
Q12. How do you find second&third largest element in an array of integers,and ?calculate sum of  these two no?
Q13. How do you find all pairs of elements in an array whose sum is equal to a given number?
Q14. How do you separate zeros from non-zeros in an integer array
Q15. How do you find continuous sub array whose sum is equal to a given number?
Q16.  How to find kth smallest element in unsorted array? )
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?
Q17.  How to find kth largest element in unsorted array? 
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, 80, 50, 40} and k = 3 then your program should return 80 because 80 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.
Q18. write a program to concatenate two arrays ?
ex:- a[]={10,20,30,40},b[]={20,40,50,70}
out put=c[]={10,20,30,40,20,40,50,70}
Q19. 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,10, 20, 80, 100}
input3 = {3, 4, 15,10, 20, 30, 70, 80, 120}
Output:10, 20, 80
Q20.  How find the first repeating element in an array of integers? 
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]


Q21. 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

Q22.  How to rearrange array in alternating positive and negative number? 
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} 
Q23.  How to find if there is a sub array with sum equal to zero? 
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.
Q24.  How to remove duplicates from array in place? 
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]. 
Q25.  How to merge sorted array? 
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.
Q26.  How to find sub array with maximum sum in an array of positive and negative number? 
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. 
Q27.  Write a program to find length of longest consecutive sequence in array of integers? 
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.
Q28. How to find minimum value in a rotated sorted array? 
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).


Q29.
Given an array of of size n and a number k, find all elements that appear more than n/k times? 
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

Q30.
 How to reverse array in place in Java?
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 :
ex :int a[]={4,5, 6,7,8}
output{ 8,7,6,5,4}

Q31.WAP to swap to arrays 
ex int a[10]={10,20,30,40,50};
int b[]={1,2,3,4,5,6,7,8,9,10};
result:
 a[10]={1,2,3,4,5,6,7,8,9,10};
b[]={10,20,30,40,50};
Q 32.  WAP to compare to two Arrays(index number of elements may be different in both arrays).
ex: int a[]={10,20,30,40,50};
      int b[]={20,50,40,10,30);
     here both arrays are same
Q33. wap to convert decimal integer into binary ?
Q34.wap to convert binary no into decimal integer?
Q35.wap to convert decimal integer into octal no?
Q36.wap to convert octal into decimal integer?
Q37.wap to make tower of Hanoi?
Q38.wap to implement stack using array?
Q39.wap to reverse an array using stack?
Q40.wap to implement a queue using array?
Q41.wap to implement circular queue using array?
Q42.wap to implement dqueue(double ended queue)?
Q43.wap to search an element in array using linear search?
Q44.wap to search an element in array using binary search?
Q45.wap to sort an array using.
a.selection sort.
b.bubble sort
c.insertion sort
d.heap sort
e.quick sort
f.merge sort
g.radix sort
h.shell sort
i.counting sort
j.bucket sort
Q46.wap to insert an element in array at user specified position?
Q47.wap to delete an element in array at user specified position?
Q48.wap to find product of two matrix's?
Q49.wap to calculate sum of upper triangle elements, lower triangle element and diagonal elements?
Q50.wap to transpose a matrix's?
Q51.wap to print only even elements of matrix's?
Q52.wap to print odd elements of matrix's?
Q53.wap to print those  elements of matrix which are prime no?
Q54.wap to calculate those elements of matrix which are prime numbers?
Q55.wap to print diagonal element only?
ex: 
1  2  3   4 
5  6  7   8 
9 10 11 12
result:
1          4
    6   7
    10 11
9           12







No comments:

Post a Comment