Q1. Java print first 100 prime number?
Q2. Print 3rd highest number in an array without using sorting?
Here we have to solve the problem without using any sorting technique and better performing code.
Here we have taken 3 variables max1, max2 and max3, which will find top 3 elements.
Find the complete code for 3rd highest variable here.
Q3. Write code to get Fibonacci series using recursion?
In the below example we have implemented Fibonacci using recursion and without recursion as well. Have a look.
Java Fibonacci ExampleQ4. Write code to reverse a string using recursion?
Here we are using the substring method for returning 1 to n elements and then adding the 0th element to the return string.
private static String reverse(String a) {
// TODO Auto-generated method stub
if(a.isEmpty()) {
return a;
}
return reverse(a.substring(1)) + a.charAt(0);
}
Q5.Find the maximum value of an array.
This question is a bit tricky, the interviewer might not ask you to use a binary search if you use its good. Else you might end up having an O(n2) solution. We have tried solving same using binary search have a look.
Find Max Using binary Search.Q6.Create your own stack with a method which will always return the smallest number of the stack.
Here we have implemented stack using an array. The tricky part here is finding the smallest number always.
Q7. Java Implement a queue using Stack
Q8. Find whether any two numbers of an array add up to k.
This question is also there in leet code. We have tried fixing it in 3 ways, have a look. two numbers of an array add up to k
ConversionConversion EmoticonEmoticon