Tricky Java Interview Questions - Part 1

Hi Folks, any java interview you go there will definitely be certain tricky question they tend to ask and sometimes even to write the program in a white board.

Here im planning to bring all those questions together in one page, so that i can be helpful for me and also you guys to refer once before the interview.

1. I had this question with a famous MNC in the 1st technical round. They gave me the below code snippet and told to write any thing after this statement and nothing before this.

Expected output : Print 1 2 3 .... 100
Code :
for(int i=0; i>

Working code Answer :

for(int i=0; i>=0;) {
if(i==100) {
break;
}
System.out.println(++i);

}

This is pretty simple but can be tough to quickly derive the logic at that point of time.

-----------------------------------------------------------------------------------------------------------------------

2. This question was asked to me during an interview to test not only my java basic programming skills but also my logical thinking.

Question : Write a logic to remove consecutive characters in a string

Samples:

aabbccdef - def
abca - abca
abba - 
aa -
bbac - ac
aabbc - c

Solution :

public class ConsecutiveStringReplace {
public static void main(String args[]) {
String input = "aabbccdef";

String output = "";
for (int j = 0; j <= input.length() / 2; j++) {
output = "";
for (int i = 0; i < input.length() - 1;) {
if (input.charAt(i) == input.charAt(i + 1)) {
i = i + 2;
if (i == input.length() - 1) {
output += Character.toString(input.charAt(i));
}
} else {
output += Character.toString(input.charAt(i));// + Character.toString(input.charAt(i+1));
i = i + 1;
if (i == input.length() - 1) {
output += Character.toString(input.charAt(i));
}
}
}
input = output;
}
System.out.println("output:" + input);
}

}



3. Count characters in a string : This problem is used to count a specific character in a string. Can be either to count totally or each character.

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.BiConsumer;

public class CountCharsInStrings {
public static void main(String args[]) {
String inputString = "sriram";
countCharsTotally(inputString);
countCharsIndividually(inputString);
List inputStringList = new ArrayList();
inputStringList.add("s");
inputStringList.add("r");
inputStringList.add("i");
inputStringList.add("r");
inputStringList.add("a");
inputStringList.add("m");
//Count number of distinct characters in a string using java8
countCharsUsingStreams(inputStringList);
}

public static void countCharsIndividually(String inputString) {
String oldVal = String.valueOf(inputString.charAt(0));
String saveString = "";
int counter = 1;
for(int i=1; i String newVal = String.valueOf(inputString.charAt(i));
if(oldVal.equals(newVal)) {
counter++;
} else {
saveString += oldVal + counter;
counter = 1;
}
if(i == inputString.length()-1) {
saveString += newVal + counter;
}
oldVal = newVal;
}
System.out.println("Individual:"+saveString);
}

public static void countCharsTotally(String inputString) {
System.out.print("Totally:");
int counter = 1;
Map testMap = new HashMap();
for(int i=0; i String charVal = String.valueOf(inputString.charAt(i));
if(testMap.containsKey(charVal)) {
counter = testMap.get(charVal);
testMap.put(charVal, ++counter);
} else {
testMap.put(charVal, 1);
}
}

BiConsumer action = (val, count) -> {
System.out.print(val+count);
};
testMap.forEach(action);
}

public static void countCharsUsingStreams(List inputStringList) {
System.out.println(inputStringList.stream().distinct().count());
}

}


4. The next program is to compare 2 arrays and output array1 - array2 and array2 - array1

import java.util.HashSet;
import java.util.Set;

public class CompareTwoArray {

//Compare 2 arrays and find out the missing ones in array1 and array2
//A - B and B - A
public static void main(String args[]) {
int arrCnt = 0;
int[] arr1 = { 1, 3, 2 };
int[] arr2 = { 1, 3, 5 };
int[] arr3 = new int[arr1.length > arr2.length ? arr1.length : arr2.length];
int[] arr4 = new int[arr1.length > arr2.length ? arr1.length : arr2.length];

Set one_set = new HashSet();
for(int i=0; i one_set.add(arr2[i]);
}

for(int i=0; i if(one_set.add(arr1[i])) {
arr3[i] = arr1[i];
}
}

System.out.println("A-B:");
for(int i=0;i if(arr3[i] != 0) {
System.out.println(arr3[i]);
}
}

System.out.println("B-A:");
for(int i=0;i if(arr4[i] != 0) {
System.out.println(arr4[i]);
}
}
}
}



5. The next program is to find the missing element in a sorted array. The solution is pretty simple, hold a new array with the size of it as the last element in the main array. In the below example its 20. Loop using the input array and set values to 1 for the values of the input array in this output array. This will set all values of the input array's index in the output array to 1.

By doing this you are leaving other elements in the output array as 0 (coz they are initialized to 0 during declaration).

public class FindMissinginSortArray {

public static void main(String args[]) {

int[] input = { 1, 2, 3, 5, 7, 9, 10, 11, 14, 16, 18, 20 };
int[] output = new int[input[input.length - 1]];

for (int i : input) {
output[i] = 1;
}

for (int i = 1; i < output.length; i++) {
if (output[i] == 0) {
System.out.println(i);
}
}
}

}


Let me know what you guys think for any other alternate solution in the comments. 
Thanks and All the best.

Comments

Popular posts from this blog

How did I pass my Kubernetes and Cloud Native Associate exam

Conscious and Sub Conscious Mind