Tuesday 11 June 2024

Java Interview Question.


1. Check if array has duplicate elements or not?

//using for loop
public boolean checkDubli(int[] input) {

for (int i = 0; i < input.length; i++) {
for (int j = i + 1; j < input.length; j++) {
if (input[i] == input[j]) {
return true;
}
}
}
return false;
}

//using hash map
public boolean checkDublicate(int[] input) {
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < input.length; i++) {
if (map.containsKey(input[i])) {
return true;
} else {
map.put(input[i], 1);
}
}
return false;
}

2. Give an algorithm for finding the element which appears maximum number of times in array.

//method 1.
public void findMAxFreqNum(int[] input) {
int count;
int maxCount = 0;
int maxRepNum = 0;
for (int j : input) {
count = 0;
for (int k : input) {
if (j == k) {
count++;
}
}
if (count > maxCount) {
maxCount = count;
maxRepNum = j;
}
}
if (maxCount == 1) {
System.out.println("all numbers are unique");
} else {
System.out.println(maxRepNum + "");
}

}
// method 2
public void findMAxFreqNum2(int[] arr) {
Arrays.sort(arr);
int count;
int maxCount = 1;
int maxFreqNum = arr[0];
for (int i = 1; i < arr.length; i++) {
count = 1;
while (arr[i] == arr[i - 1]) {
count++;
i++;
}
if (count > maxCount) {
maxCount = count;
maxFreqNum = arr[i-1];
}
}
if (maxCount == 1) {
System.out.println("All numbers are unique");
} else {
System.out.println(maxFreqNum);
}
}

3. In array find the first element in the array which is repeated.


//using map
public int firstElementRep1(int[] arr1) {
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < arr1.length; i++) {
if (map.containsKey(arr1[i])) {
return arr1[i];
} else {
map.put(arr1[i], 1);
}
}
return 0;
}

4. Given an array of n element, find two elements in the array such that their sum is equal to given element K.

public void findElementSum(int[] arr, int k) {
int sum = 0;
for (int i = 0; i < arr.length; i++) {
for (int j = i + 1; j < arr.length; j++) {
sum = arr[i] + arr[j];
if (sum == k) {
System.out.println("two element are:" + arr[i] + " , " + arr[j] + " = " + k);
}
}
}
}

5. Find three element in the arrya such that their sum is equal to given element K.

public void findThreeNumSum(int[] arr, int s) {
int sum;
for (int i = 0; i < arr.length; i++) {
for (int k = i + 1; k < arr.length; k++) {
for (int j = k + 1; j < arr.length; j++) {
sum = arr[i] + arr[k] + arr[j];
if (sum == s) {
System.out.println(
"Three elements are:" + arr[i] + " , " + arr[k] + " , " + arr[j] + " = " + s);
}
}

}
}
}

6.Given a sorted array of n integer that has been rotated an unknown number of times, give a O(logn) algorith that finds an element in the array.

// First learn Binary Search

public int searchNum(int[] arr, int k) {
int mid;
int l = 0, r = arr.length - 1;
while (l <= r) {
mid = l + ((r - l) / 2);
if (k == arr[mid]) {
return mid;
} else if (k > arr[mid]) {
l = mid + 1;
} else if (k < arr[mid]) {
r = mid - 1;
}
}
return -1;
}
//Actual code is pending

7. Given a sorted array A of n element, possibly with duplicates. Find the index of the first occurrence of a number in O(logn) time.

public int findDupNumFirstPos(int[] arr, int num) {
int l = 0, r = arr.length - 1;
int mid;
int searchItemPos = -1;
while (l <= r) {
mid = l + ((r - l) / 2);
if (num == arr[mid]) {
searchItemPos = mid;
r = mid - 1;
} else if (num > arr[mid]) {
l = mid + 1;
} else if (num < arr[mid]) {
r = mid - 1;
}
}
return searchItemPos;
}

8. Separate even and odd number in array. Put all even number first and then odd number.

public void sepEvenOdd(int[] arr) {
int l = 0, r = arr.length - 1, temp;
while (l < r) {
if (arr[l] % 2 == 0) {
l++;
}
while (arr[r] % 2 != 0) {
r--;
}
if (l < r) {
temp = arr[l];
arr[l] = arr[r];
arr[r] = temp;
}
}
for (int k : arr) {
System.out.print(k + " , ");
}
}

9 Sort an array of 0,1 and 2.

// method 1 simple way.

public void sortZeroOneTwo(int[] arr) {
Arrays.sort(arr);
for (int i : arr) {
System.out.print(i + " ");
}
}

// using loop
public void sortZeroOneTwo2(int[] arr) {
int count0 = 0, count1 = 0, count2 = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i] == 0) {
count0++;
} else if (arr[i] == 1) {
count1++;
} else if (arr[i] == 2) {
count2++;
}
}
for(int i=0;i<count0-1;i++){
arr[i]=0;
}
for (int i=count0;i<count0+count1;i++){
arr[i]=1;
}
for (int i=count0+count1;i<count0+count1+count2;i++){
arr[i]=2;
}
System.out.println();
for (int i:arr){
System.out.print(i+" ");
}
}