Top 30 Coding Interview Questions You Should Know !

Here are the Top 30 Coding Interview Questions with different IT companies:

Are you a recent graduate looking to start a career as a Java developer? Or are you an experienced programmer wanting to make the switch to Java? In any event, you will almost probably be required to attend a job interview. To help you prepare for and ace your interview, we’ve compiled a thorough collection of 30 Core Java interview questions covering a wide range of subjects. This lesson will give you the facts and confidence you need to ace your Java interview.

notes Top 30 Coding Interview Questions You Should Know !
College Notes – for all university students – Updategadh
College Notes – for university students | Discover the ultimate exam preparation resources at CollegeNotes!
cropped-Ug-logo-1-32x32 Top 30 Coding Interview Questions You Should Know !updategadh.com

Basic Java Concepts:

  1. “Write a Java program to find the sum of two numbers (Microsoft 2023).”
  2. “Calculate the area of a circle in Java given its radius (Google 2023).”
  3. “Implement a program to convert temperature from Fahrenheit to Celsius (Amazon 2023).”
  4. “Create a Java program to check whether a given number is prime or not (Apple 2023).”
  5. “Write code to reverse a string in Java (Facebook 2023).”
  6. “Implement a program to find the factorial of a number using recursion (Microsoft 2023).”
  7. “Calculate the sum of natural numbers up to a given limit using a loop (Google 2023).”
  8. “Create a Java program to find the largest among three numbers (Amazon 2023).”
  9. “Write code to check if a given string is a palindrome or not (Apple 2023).”
  10. “Implement a Java program to count the number of vowels in a string (Facebook 2023).”
image Top 30 Coding Interview Questions You Should Know !
Core Java Interview Questions For Freshers: Master the Fundamentals with Confidence!
Core Java Interview Questions For Freshers |
cropped-Ug-logo-1-32x32 Top 30 Coding Interview Questions You Should Know !updategadh.com

Variables and Data Types:

  1. “Calculate the power of a number using recursion (Microsoft 2023).”
  2. “Write a Java program to find the intersection of two arrays (Google 2023).”
  3. “Implement a program to find the LCM (Least Common Multiple) of two numbers (Amazon 2023).”
  4. “Create code to find the second largest element in an array (Apple 2023).”
  5. “Write a program to rotate an array to the right by ‘k’ steps (Facebook 2023).”

Operators and Expressions:

  1. “Implement a Java program to check if a string contains all unique characters (Microsoft 2023).”
  2. “Calculate the sum of all elements in a 2D array (Google 2023).”
  3. “Write code to sort an array of integers in ascending order (Amazon 2023).”
  4. “Create a program to find the number of words in a string (Apple 2023).”
  5. “Implement a Java program to find the missing number in an array of integers (Facebook 2023).”
 Top 30 Coding Interview Questions You Should Know !
 Top 30 Coding Interview Questions You Should Know !

Advanced Coding Questions:

  1. “Calculate the square root of a number using the Newton-Raphson method (Microsoft 2023).”
  2. “Write code to merge two sorted arrays into a single sorted array (Google 2023).”
  3. “Create a program to find the longest substring without repeating characters (Amazon 2023).”
  4. “Implement a Java program to check if a number is a perfect number or not (Apple 2023).”
  5. “Write a program to find the common elements between two arrays (Facebook 2023).”
  6. “Calculate the factorial of a number without using recursion (Microsoft 2023).”
  7. “Create code to find the majority element in an array (Google 2023).”
  8. “Implement a Java program to perform matrix multiplication (Amazon 2023).”
  9. “Write code to count the number of occurrences of a character in a string (Apple 2023).”
  10. “Calculate the sum of all even numbers up to a given limit using a loop (Facebook 2023).”
image-62 Top 30 Coding Interview Questions You Should Know !
Coding Interview Questions
coding interview questions
java coding interview questions
Coding Interview Questions

Solution -Coding Interview Questions

Question 1: Write a Java program to find the sum of two numbers.

import java.util.Scanner;

public class SumOfTwoNumbers {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter the first number: ");
        double num1 = scanner.nextDouble();
        System.out.print("Enter the second number: ");
        double num2 = scanner.nextDouble();
        double sum = num1 + num2;
        System.out.println("Sum: " + sum);
    }
}

Question 2: Calculate the area of a circle in Java given its radius .

import java.util.Scanner;

public class CircleArea {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter the radius of the circle: ");
        double radius = scanner.nextDouble();
        double area = Math.PI * Math.pow(radius, 2);
        System.out.println("Area of the circle: " + area);
    }
}

Question 3: Implement a program to convert temperature from Fahrenheit to Celsius .

import java.util.Scanner;

public class FahrenheitToCelsius {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter temperature in Fahrenheit: ");
        double fahrenheit = scanner.nextDouble();
        double celsius = (fahrenheit - 32) * 5/9;
        System.out.println("Temperature in Celsius: " + celsius);
    }
}

Question 4: Create a Java program to check whether a given number is prime or not .

import java.util.Scanner;

public class PrimeNumberChecker {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a number: ");
        int num = scanner.nextInt();
        boolean isPrime = true;

        if (num <= 1) {
            isPrime = false;
        } else {
            for (int i = 2; i <= Math.sqrt(num); i++) {
                if (num % i == 0) {
                    isPrime = false;
                    break;
                }
            }
        }

        if (isPrime) {
            System.out.println(num + " is a prime number.");
        } else {
            System.out.println(num + " is not a prime number.");
        }
    }
}

Question 5: Write code to reverse a string in Java.

public class ReverseString {
    public static void main(String[] args) {
        String original = "Hello, World!";
        String reversed = new StringBuilder(original).reverse().toString();
        System.out.println("Original: " + original);
        System.out.println("Reversed: " + reversed);
    }
}

Question 6: Implement a program to find the factorial of a number using recursion.

public class Factorial {
    public static void main(String[] args) {
        int number = 5; // Change this to the desired number
        long factorial = findFactorial(number);
        System.out.println("Factorial of " + number + " is " + factorial);
    }

    public static long findFactorial(int n) {
        if (n == 0) {
            return 1;
        }
        return n * findFactorial(n - 1);
    }
}

Question 7: Calculate the sum of natural numbers up to a given limit using a loop .

import java.util.Scanner;

public class SumOfNaturalNumbers {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a positive integer: ");
        int n = scanner.nextInt();
        int sum = 0;

        for (int i = 1; i <= n; i++) {
            sum += i;
        }

        System.out.println("Sum of natural numbers up to " + n + " is " + sum);
    }
}

Question 8: Create a Java program to find the largest among three numbers .

import java.util.Scanner;

public class LargestAmongThree {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter the first number: ");
        double num1 = scanner.nextDouble();
        System.out.print("Enter the second number: ");
        double num2 = scanner.nextDouble();
        System.out.print("Enter the third number: ");
        double num3 = scanner.nextDouble();

        double largest = Math.max(Math.max(num1, num2), num3);

        System.out.println("The largest number is: " + largest);
    }
}

Question 9: Write code to check if a given string is a palindrome or not .

public class PalindromeChecker {
    public static void main(String[] args) {
        String str = "racecar"; // Change this to the desired string
        boolean isPalindrome = checkPalindrome(str);

        if (isPalindrome) {
            System.out.println(str + " is a palindrome.");
        } else {
            System.out.println(str + " is not a palindrome.");
        }
    }

    public static boolean checkPalindrome(String str) {
        str = str.replaceAll("[^a-zA-Z0-9]", "").toLowerCase();
        int left = 0;
        int right = str.length() - 1;

        while (left < right) {
            if (str.charAt(left) != str.charAt(right)) {
                return false;
            }
            left++;
            right--;
        }

        return true;
    }
}
maxresdefault Top 30 Coding Interview Questions You Should Know !
Coding Interview Questions

Question 10: Implement a Java program to count the number of vowels in a string

import java.util.Scanner;

public class VowelCounter {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a string: ");
        String input = scanner.nextLine();
        int vowelCount = countVowels(input);
        System.out.println("Number of vowels in the string: " + vowelCount);
    }

    public static int countVowels(String str) {
        str = str.toLowerCase();
        int count = 0;

        for (int i = 0; i < str.length(); i++) {
            char ch = str.charAt(i);
            if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
                count++;
            }
        }

        return count;
    }
}

Read more :Coding Interview Questions

See also  Top 20 OOPs Interview Questions and Answers (2025)

1. Calculate the power of a number using recursion:

public class PowerCalculator {
    public static int calculatePower(int base, int exponent) {
        if (exponent == 0) {
            return 1;
        } else {
            return base * calculatePower(base, exponent - 1);
        }
    }

    public static void main(String[] args) {
        int base = 2;
        int exponent = 3;
        int result = calculatePower(base, exponent);
        System.out.println(base + " ^ " + exponent + " = " + result);
    }
}

2. Write a Java program to find the intersection of two arrays:

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

public class ArrayIntersection {
    public static void main(String[] args) {
        int[] arr1 = {1, 2, 3, 4, 5};
        int[] arr2 = {3, 4, 5, 6, 7};

        Set<Integer> set1 = new HashSet<>();
        Set<Integer> set2 = new HashSet<>();

        for (int num : arr1) {
            set1.add(num);
        }

        for (int num : arr2) {
            if (set1.contains(num)) {
                set2.add(num);
            }
        }

        int[] intersection = new int[set2.size()];
        int index = 0;
        for (int num : set2) {
            intersection[index++] = num;
        }

        System.out.println("Intersection of two arrays: " + Arrays.toString(intersection));
    }
}

3. Implement a program to find the LCM (Least Common Multiple) of two numbers:

public class LCMCalculator {
    public static int calculateLCM(int num1, int num2) {
        int max = Math.max(num1, num2);
        int min = Math.min(num1, num2);
        int lcm = max;

        while (lcm % min != 0) {
            lcm += max;
        }

        return lcm;
    }

    public static void main(String[] args) {
        int num1 = 12;
        int num2 = 18;
        int lcm = calculateLCM(num1, num2);
        System.out.println("LCM of " + num1 + " and " + num2 + " is " + lcm);
    }
}

4. Create code to find the second largest element in an array:

public class SecondLargestElement {
    public static int findSecondLargest(int[] arr) {
        int max = Integer.MIN_VALUE;
        int secondMax = Integer.MIN_VALUE;

        for (int num : arr) {
            if (num > max) {
                secondMax = max;
                max = num;
            } else if (num > secondMax && num < max) {
                secondMax = num;
            }
        }

        return secondMax;
    }

    public static void main(String[] args) {
        int[] arr = {10, 5, 8, 20, 15};
        int secondLargest = findSecondLargest(arr);
        System.out.println("The second largest element is: " + secondLargest);
    }
}

5. Write a program to rotate an array to the right by ‘k’ steps:

public class ArrayRotation {
    public static void rotateArray(int[] arr, int k) {
        int n = arr.length;
        k = k % n; // Handle cases where k is greater than the array length

        int[] result = new int[n];
        for (int i = 0; i < n; i++) {
            result[(i + k) % n] = arr[i];
        }

        System.arraycopy(result, 0, arr, 0, n);
    }

    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 4, 5};
        int k = 2; // Number of steps to rotate
        rotateArray(arr, k);
        System.out.println("Array after rotating by " + k + " steps: " + Arrays.toString(arr));
    }
}
image-63 Top 30 Coding Interview Questions You Should Know !
Coding Interview Questions

1. Implement a Java program to check if a string contains all unique characters:

public class UniqueCharactersChecker {
    public static boolean hasAllUniqueCharacters(String str) {
        if (str.length() > 128) {
            return false; // If the string is longer than the number of possible unique characters
        }

        boolean[] charSet = new boolean[128];
        for (int i = 0; i < str.length(); i++) {
            int val = str.charAt(i);
            if (charSet[val]) {
                return false; // Character already found in the string
            }
            charSet[val] = true;
        }
        return true;
    }

    public static void main(String[] args) {
        String input = "abcdefg";
        boolean result = hasAllUniqueCharacters(input);
        System.out.println("String has all unique characters: " + result);
    }
}

2. Calculate the sum of all elements in a 2D array:

public class ArraySumCalculator {
    public static int calculateArraySum(int[][] array) {
        int sum = 0;
        for (int[] row : array) {
            for (int num : row) {
                sum += num;
            }
        }
        return sum;
    }

    public static void main(String[] args) {
        int[][] twoDArray = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
        int sum = calculateArraySum(twoDArray);
        System.out.println("Sum of all elements in the 2D array: " + sum);
    }
}

3. Write code to sort an array of integers in ascending order:

import java.util.Arrays;

public class ArraySorter {
    public static void sortArray(int[] arr) {
        Arrays.sort(arr);
    }

    public static void main(String[] args) {
        int[] arr = {5, 3, 8, 1, 2};
        sortArray(arr);
        System.out.println("Sorted array in ascending order: " + Arrays.toString(arr));
    }
}

4. Create a program to find the number of words in a string:

public class WordCounter {
    public static int countWords(String str) {
        String[] words = str.split("\\s+");
        return words.length;
    }

    public static void main(String[] args) {
        String input = "This is a sample sentence with words.";
        int wordCount = countWords(input);
        System.out.println("Number of words in the string: " + wordCount);
    }
}

5. Implement a Java program to find the missing number in an array of integers:

public class MissingNumberFinder {
    public static int findMissingNumber(int[] arr, int n) {
        int total = n * (n + 1) / 2;
        for (int num : arr) {
            total -= num;
        }
        return total;
    }

    public static void main(String[] args) {
        int[] arr = {1, 2, 4, 6, 3, 7, 8};
        int n = 8; // Range of numbers from 1 to n
        int missingNumber = findMissingNumber(arr, n);
        System.out.println("The missing number in the array is: " + missingNumber);
    }
}
image-61 Top 30 Coding Interview Questions You Should Know !
Complete Java Course with Real Projects – Updategadh
Complete Java Course
cropped-Ug-logo-1-32x32 Top 30 Coding Interview Questions You Should Know !updategadh.com

  1. Calculate the square root of a number using the Newton-Raphson method:
public class SquareRootCalculator {
    public static double calculateSquareRoot(double num) {
        double x = num;
        double epsilon = 1e-15; // A small value for precision
        while (Math.abs(x - num / x) > epsilon * x) {
            x = (x + num / x) / 2.0;
        }
        return x;
    }

    public static void main(String[] args) {
        double number = 16.0; // Change this to the number you want to find the square root of.
        double result = calculateSquareRoot(number);
        System.out.println("Square root of " + number + " is approximately " + result);
    }
}
  1. Write code to merge two sorted arrays into a single sorted array:
import java.util.Arrays;

public class MergeSortedArrays {
    public static int[] mergeSortedArrays(int[] arr1, int[] arr2) {
        int[] result = new int[arr1.length + arr2.length];
        int i = 0, j = 0, k = 0;

        while (i < arr1.length && j < arr2.length) {
            if (arr1[i] < arr2[j]) {
                result[k++] = arr1[i++];
            } else {
                result[k++] = arr2[j++];
            }
        }

        while (i < arr1.length) {
            result[k++] = arr1[i++];
        }

        while (j < arr2.length) {
            result[k++] = arr2[j++];
        }

        return result;
    }

    public static void main(String[] args) {
        int[] arr1 = {1, 3, 5, 7};
        int[] arr2 = {2, 4, 6, 8};
        int[] mergedArray = mergeSortedArrays(arr1, arr2);
        System.out.println("Merged sorted array: " + Arrays.toString(mergedArray));
    }
}
  1. Create a program to find the longest substring without repeating characters:
See also  Top 10 SQL Interview Questions and Answers

You can use the sliding window technique to solve this problem. Here’s a sample solution:

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

public class LongestSubstringWithoutRepeatingChars {
    public static int lengthOfLongestSubstring(String s) {
        int n = s.length();
        int maxLen = 0;
        int i = 0, j = 0;
        Set<Character> set = new HashSet<>();

        while (j < n) {
            if (!set.contains(s.charAt(j))) {
                set.add(s.charAt(j));
                maxLen = Math.max(maxLen, j - i + 1);
                j++;
            } else {
                set.remove(s.charAt(i));
                i++;
            }
        }

        return maxLen;
    }

    public static void main(String[] args) {
        String input = "abcabcbb"; // Change this to your input string.
        int result = lengthOfLongestSubstring(input);
        System.out.println("Length of the longest substring without repeating characters: " + result);
    }
}

2 comments

Post Comment