Top 50 Java interview coding problems and Solutions ?

Top 50 Java interview coding problems and Solutions ?

So while preparing for an interview i have practiced coding questions, this is the list of coding questions that I will be solving.

  1. print Fibonacci using iteration --> 1 1 2 3 5 8 13
class test {
    public static void main(String args[]) {

        // print 1 1 2 3 5 8 13

        for (int i = 1; i <= 5; i++)
            System.out.println(fibonacciIteration(i));
    }

    public static int fibonacciIteration(int n) {
        if (n == 1 || n == 2) {
            return 1;
        }

        int first = 1;
        int second = 1;
        int sum = 0;
        for (int i = 3; i <= n; i++) {
            sum = first + second;
            first = second;
            second = sum;
        }
        return sum;
    }
}
  1. Check palindrome in java using inbuild functions.


public class test {

    public static void main(String[] args) {

        System.out.println(checkPalindrome("aca"));
    }

    public static boolean checkPalindrome(String s) {

        String s1 = s;
        StringBuilder s2 = new StringBuilder(s);
        s2.reverse();

        String s3 = s2.toString();

        if (s1.equals(s3))
            return true;

        else
            return false;

    }
}
  1. Reversing a string using stack
import java.util.Stack;

public class test {

    public static void main(String[] args) {

        ReverseStringManually("abcd");
    }

    public static void ReverseStringManually(String s) {

        Stack<Character> stack1 = new Stack<>();

        for (int i = 0; i < s.length(); i++) {

            char x = s.charAt(i);

            stack1.add(x);

        }

        while (!stack1.isEmpty())
            System.out.print(stack1.pop());

    }

}
  1. How to reverse a Array in place ?
public class test {
    public static void main(String[] args) {
        System.out.println("hello world");

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

        reverseArrayInPlace(arr);
    }

    public static void reverseArrayInPlace(int arr[]) {

        int first = 0;
        int last = arr.length - 1;

        while (first < last) {
            int temp = arr[first];
            arr[first] = arr[last];
            arr[last] = temp;

            first = first + 1;
            last = last - 1;
        }

        for (int a : arr) {
            System.out.print(a + " ");
        }
    }
}
  1. How to reverse Each letter of a string

  2. Create one function to split the string into array using s.split(" ")
  3. create one function to reverse a word and then call the reverse function for each array element and append that to the final string and give the output in the response.
  1. How to search a element using binary search ?

condition here is that the array should be sorted.


import javax.lang.model.util.ElementScanner6;

/**
 * test
 */
public class test {

    public static void main(String[] args) {

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

        int element = 6;

        System.out.println(findElementBinarySearch(array, element));
    }

    public static boolean findElementBinarySearch(int arr[], int element) {

        int left = 0;
        int right = arr.length - 1;

        int mid = 0;

        while (left < right) {
            mid = (left + right) / 2;

            if (element < arr[mid]) {
                right = mid - 1;

            } else if (element > arr[mid]) {
                left = mid + 1;
            } else if (element == arr[mid]) {
                return true;

            }

        }

        return false;

    }
}
  1. Printing patterns

  2. Rectangle

 public static void printRectangle(int l, int b) {

        for (int i = 0; i < l; i++) {
            for (int j = 0; j < b; j++) {
                System.out.print("* ");
            }
            System.out.println();
        }
    }
* * * * *
* * * * *
* * * * *
  • Hollow Rectangle :
  • pyramid
*
* *
* * *
* * * *
* * * * *


    public static void halfPyramid(int l) {

        for (int i = 0; i < l; i++) {

            for (int j = 0; j <= i; j++) {

                System.out.print("* ");

            }

            System.out.println();

        }

    }
  1. Remove duplicates from an array (solution)

  2. Printing patterns (solutions)

  3. Print repeated characters of String? (solution)

  4. GCD of two numbers (solution)

  5. The square root of a number (solution)

  1. Reverse words of a sentence (solution)

  2. Leap year (solution)

  3. Binary search (solution)

  4. String Anagram (solution)

  1. The first non-repeated character of String (solution)

  2. Finding Middle element of linked list in one pass (solution)

  3. Pre-order traversal (solution)

  4. Pre-order traversal without recursion (solution)

  5. In order traversal (solution)

  6. In order traversal without recursion (solution)

  7. Post-order traversal (solution)

  8. Postorder traversal without recursion (solution)

  9. Print all leaves of a binary tree (solution)

  1. Sort array using quicksort (solution) You need to write a Java program to sort an array of integers using a quick sort algorithm. You cannot use any library method, e.g. JDK or a third party library, which means, you need to first implement the quicksort algorithm and then sort the array.
  1. Insertion sort (solution) Write a program to implement the insertion sort algorithm in Java. The program should take an unsorted array and sort it using insertion sort algorithm Also explain the best case and worst case time and space complexity of the Insertion sort algorithm.

  2. Bubble sort (solution) Write a program to implement the bubble sort algorithm in Java. You can use basic operators and functions, but sorting functions from Java API is not allowed.

  3. Transpose a matrix (solution)

  4. Print all permutations of String (solution) Write a Java program to print all permutations of a given String. For example, if given String is "GOD" then your program should print all 6 permutations of this string, e.g. "GOD," "OGD," "DOG," "GDO," "ODG," and "DGO."

Java Programming Interview Questions for 2 to 3 years

  1. Adding two matrices in Java (solution)

  2. Matrix multiplication (solution)

  3. Reverse a linked list (solution)

Java Programming Interview Questions for experienced

  1. Find the length of the linked list (solution) Just write a program in Java to find the length of a singly linked list in one pass, i.e. in just one iteration of a singly linked list.
  1. Check if a linked list has a loop (solution) Write a program to check if given linked list has a loop or not. Sometimes a linked list get corrupt, and two nodes point to the same node, which forms the loop or cycle in the linked list.

  2. Find the start of loop in a linked list (solution)

  3. Find the middle element of a linked list (solution)

  4. Find the 3rd element from the tail linked list (solution) You need to write a program to find the 3rd element from the tail of a singly linked list. You need to solve this problem without iterating twice. If you want more linked list questions, you can see the article about frequently asked linked list interview questions.

Java Coding Interview Questions

  1. Convert a linked list to a binary tree (solution) It's possible to convert a doubly-linked list to a binary tree, you need to write a Java program which takes a doubly-linked list and returns a binary tree.
  1. Sort a linked list (solution) You need to given an unsorted linked list, and you need to write a program in Java to sort them in ascending order of the values in each node.
  1. Iterative Quicksort (solution) You need to write a Java program to implement quicksort sorting algorithm without recursion. You can use essential JDK classes and programming constructs, but recursion is not allowed.
  1. Bucket sort (solution) This program is increasingly getting popular on Java interview because it sorts a given array in linear time. Though there is a lot of prerequisites, e.g. you must know the maximum value present in the array, it is a very interesting problem from interview point of view. You need to write a program to implement a bucket sort algorithm in Java. If you are not familiar with Bucket sort or any other linear sorting algorithm, I suggest you to first read a good on algorithms, e.g. Introduction to Algorithms by Thomas H. Cormen.

Top 50 Java Programs from Coding Interviews

  1. Counting sort (solution) This is another problem which is similar to the previous one because counting sort is also a linear sorting algorithm. Just remember that bucket sort, and counting sort are different algorithms, so it's also good to state how they are different.
  1. Check if two string rotation of each other Write a program which accepts two given String and checks if they are the rotation of each. If they then return true otherwise return false. A String is said to be a rotation of other string if they contain same characters and the sequence is rotated across any character, e.g. "dabc" is a rotation of "abcd" but "dbac" is not. If you want to practice more string-based questions, you can also see my list of 20 String-based algorithm questions from Java interviews.
  1. LRU cache in Java (solution) Write a program to implement an LRU cache in Java. An LRU cache means Least Recently Used Cache which removes the least recently used element if the cache is full. You can use LinkedHashMap to implement LRU cache in Java.

Java Programming Interview Questions

  1. Merge sort