Hi, There
In this article, I have writing the Top 10 popular Java interview coding challenges questions with answers. This might be helpful, who is looking for JAVA developer jobs having experience as well as for fresher.

1) Swap Numbers

In this challenge, we need to write a logic that swaps the value of each other. Here I am solving this in two ways,
1) By using the third variable
2) Without a third variable

package com.example;

public class SwapNumbers {
    public static void main(String[] args) {
        int x = 10;
        int y = 20;

        //1. Using third variable
        int temp = x;
        x = y;
        y = temp;
        System.out.println(x + "" + y);


        //without third variable
        x = 10;
        y = 20;

        x = x + y;
        y = x - y;
        x = x - y;
        System.out.println(x + "" + y);

    }
}

2) Find Second Largest Number in Array

In this challenge, we need to write a logic that prints the second largest number in the given array.

package com.example;

import java.util.Arrays;

public class SecondLargestArray {
    public static void main(String[] args) {
        int intArray[] = {23, 43, 45, 56, 22, 38, 78};

        Arrays.sort(intArray);
        System.out.println(intArray[intArray.length - 2]);
    }
}

3) Reverse String

In this challenge, we need to write a logic that reverses the given string. Here I am doing this by two ways.
1) By using the StringBuilder reverse method.
2) By converting to the char array.

package com.example;

public class ReverseString {

    private  static String reverseString(String str) {
        char charArray[] = str.toCharArray();
        String reverseStr = "";
        for (int i = charArray.length; i > 0; i--) {
            reverseStr += charArray[i - 1];
        }
        return reverseStr;
    }

    public static void main(String[] args) {

      // 1. First way
        String s = "howtogetjob";
        StringBuilder builder = new StringBuilder();
        builder.append(s);
        System.out.println(builder.reverse());

        // 2. Second way
        System.out.println(ReverseString.reverseString(s));;
    }
}

4) Convert List into Map having Value as last 4 characters of the string.

In this challenge, we need to write a logic that prints the map having key as a string and the value of the last 4 characters of the string.

package com.example;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class LastFiveCharacter {

    private static Map<String, String> changeIntoFiveLetter(List<String> listString) {

        Map<String, String> mapString = new HashMap<>();

        listString.forEach(name -> {
            if (name.length() > 5) {
                mapString.put(name, name.substring(name.length() - 5));
            } else {
                mapString.put(name, name);
            }

        });

        return mapString;
    }


    public static void main(String[] args) {

        List<String> stringList = Arrays.asList("Harish","naryan","Shyam","acharya");
        System.out.println(LastFiveCharacter.changeIntoFiveLetter(stringList));

    }

}

5) FizzBuzz

FizzBuzz is a fun game mostly played in elementary school. The rules are simple: when your turn arrives, you say the next number. However, if that number is a multiple of five, you should say the word “fizz” (preferably with a French accent) instead. If the number is a multiple of seven, you should say “buzz.” And if it is a multiple of both, you should say “fizzbuzz.” If you mess up, you’re out, and the game continues without you.

In this challenge, we need to write a logic that prints the above game.
Here I am using three ways to resolve this.
1) By using java 8 IntStream
2) For each with simple if-else condition
3) For each with if-else condition.

package com.example;

import java.util.stream.IntStream;

public class FizzBizz {

    public static void printFizBizz(int num) {
        IntStream.rangeClosed(1, num).mapToObj(i -> (i % 5 == 0 ? (i % 7 == 0 ? "FizzBuzz" : "Fizz") : (i % 7 == 0 ? "Buzz" : i))).forEach(System.out::print);
    }

    public static void printFizzBuzzJava7(int num) {
        for (int i = 1; i < num + 1; i++) {
            if (i % 5 == 0) {
                if (i % 7 == 0) {
                    System.out.print("FizzBuzz");
                } else {
                    System.out.print("Fizz");
                }
            } else if (i % 7 == 0) {
                System.out.print("Buzz");
            } else {
                System.out.print(i);
            }
        }
    }

    public static void printFizzBuzzJava7next(int num) {
        for (int i = 1; i < num + 1; i++) {
            if (i % 5 == 0 && i % 7 == 0) {
                System.out.print("FizzBuzz");
            } else if (i % 5 == 0) {
                System.out.print("Fizz");
            } else if (i % 7 == 0) {
                System.out.print("Buzz");
            } else {
                System.out.print(i);
            }
        }
    }


    public static void main(String[] args) {

        printFizBizz(100);
        System.out.println();
        printFizzBuzzJava7(100);
        System.out.println();
        printFizzBuzzJava7next(100);
        System.out.println();

    }
}

6) Check Prime Number

In this challenge, we need to write a logic that finds the given number is prime or not.

package com.example;

public class FindPrime {

    public static void main(String[] args) {

        int num = 17;
        boolean isPrime = true;

        for (int i = 2; i <= num / 2; i++) {
            int temp = num % i;
            if (temp == 0) {
                isPrime = false;
                break;
            }
        }
        if (isPrime) System.out.println(num + " is prime");
        else System.out.println(num + " is not a prime ");

    }
}

7) Fibonacci Series

In the Fibonacci series, the next number is the sum of the previous two numbers for example 0, 1, 1, 2, 3, 5, 8, 13, etc. The first two numbers of the Fibonacci series are 0 and 1.
In this challenge, we need to write a logic that prints the Fibonacci series of a given number. Here, I am doing this by two ways,
1) By using recursion
2) Without recursion

package com.example;

public class Fibonacci_series {

    public static int fibonacci2(int number) {
        if (number == 1 || number == 2) {
            return 1;
        }
        int fibo1 = 1, fibo2 = 1, fibonacci = 1;
        for (int i = 3; i <= number; i++) {

            //Fibonacci number is sum of previous two Fibonacci number
            fibonacci = fibo1 + fibo2;
            fibo1 = fibo2;
            fibo2 = fibonacci;

        }
        return fibonacci; 

    }

    //by using recursive
    public static int fibonacci(int number) {
        if (number == 1 || number == 2) {
            return 1;
        }
        return fibonacci(number - 1) + fibonacci(number - 2); //tail recursion
    }

    //simple Method
    public static void simpleMethod(int num) {
        int a = 0, b = 0, c = 1;
        for (int i = 0; i <= num; i++) {
            a = b;
            b = c;
            c = a + b;

            System.out.print(a + " ");
        }
    }


    public static void main(String[] args) {

        int number = 10;
        for (int i = 1; i <= number; i++) {
            System.out.print(fibonacci2(i) + " ");
        }
        System.out.println("");

        for (int i = 1; i <= number; i++) {
            System.out.print(fibonacci(i) + " ");
        }

        System.out.println(" ");
        simpleMethod(number);


    }
}

8) Count the number of words from the sentence.

In this challenge, we need to write a logic that prints the number of occurrences of the word in map key-value form of the given sentence.

package com.example;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class Counting_The_Number_of_words {


    public static void byUsingHashMap(String sentence) {
        String strArray[] = sentence.split(" ");
        Map<String, Integer> mapData = new HashMap<>();
        Arrays.asList(strArray).forEach(word -> {
            if (mapData.containsKey(word)) {
                int count = mapData.get(word);
                mapData.put(word, count+1);
            } else {
                mapData.put(word, 1);
            }

        });
        System.out.println(mapData);

    }

    public static void main(String[] args) {
        String str= "I would like to work on java I would like to play coding game I I ";
        Counting_The_Number_of_words.byUsingHashMap(str);
        System.out.println(str.split(" ").length);
    }
}

9) Armstrong Check

A positive number is called Armstrong number if it is equal to the sum of cubes of its digits for example 0, 1, 153, 370, 371, 407, etc.

In this challenge, we need to write a logic that checks the given number is Armstrong or not.

package com.example;

public class ArmstrongCheck {

    private static boolean checkArmstrong(int num) {

        int lastNumber, cubeNumber = 0, orgNum;
        orgNum = num;
        while (num > 0) {

            lastNumber = num % 10;
            cubeNumber += (lastNumber * lastNumber * lastNumber);
            num = num / 10;
        }
        return cubeNumber == orgNum;
    }

    public static void main(String[] args) {

        int a, c = 0, temp;
        int n = 153;
        temp = n;

        while (n > 0) {
            a = n % 10;
            n = n / 10;
            c = c + (a * a * a);
        }
        if (temp == c) {
            System.out.println("Armstrong Number");

        } else System.out.println("not armstrong");

        System.out.println(ArmstrongCheck.checkArmstrong(temp));
    }
}

10) Anagram

 An anagram of a string is another string that contains the same characters, only the order of characters can be different. For example, “abcd” and “dabc” are an anagram of each other.

In this challenge, we need to write a logic that checks the given strings are anagram or not.

package com.example;


import java.util.Arrays;
import java.util.List;

public class AnagramExample {


    private static boolean remAnagram(String str1, String str2) {

        int[] count1 = new int[26];
        int[] count2 = new int[26];

        for (int i = 0; i < str1.length(); i++)
            count1[str1.charAt(i) - 'a']++;

        for (int i = 0; i < str2.length(); i++)
            count2[str2.charAt(i) - 'a']++;

        int result = 0;
        for (int i = 0; i < 26; i++)
            result += Math.abs(count1[i] -
                    count2[i]);
        return result==0;
    }

    public static void main(String[] args) {

        String str1 = "abce", str2 = "ecba";
        System.out.println(remAnagram(str1, str2));

    }
}
Fell free to add questions/comments in the below comments section.

Leave a Reply

Your email address will not be published. Required fields are marked *