Here are some of the most popular programming interview Question with solutions

Author:Prateek Jain


programming interview questions

Solutions are coded in java programming language.


1) How to reverse a string?

Using Recursion


public class Test {
    public static void main(String[] a) {
        String initialValue = "Tech Wealth Buzz";
        String reversedValue = reverseIt(initialValue);
        System.out.println(reversedValue);
    }

    public static String reverseIt(String str) {
        if (str.isEmpty()) {
            return str;
        } else {
            return reverseIt(str.substring(1)) + str.charAt(0);
        }
    }
}


using StringBuilder


public class Test {
    public static void main(String[] args) {
        String initialValue = "Tech Wealth Buzz";
        StringBuilder reversedValue = new StringBuilder(initialValue).reverse();
        String result = reversedValue.toString();
        System.out.println(result);
    }
}


using loop to reverse character by character


public class Test {
    public static void main(String[] args) {
        String initialValue = "Tech Wealth Buzz";
        String reversedValue = reverseIt(initialValue);
        System.out.println(reversedValue);
    }

    public static String reverseIt(String str) {
        StringBuilder reversed = new StringBuilder();
        for (int i = str.length() - 1; i >= 0; i--) {
            reversed.append(str.charAt(i));
        }
        return reversed.toString();
    }
}



2) How to check if a string is a palindrome

To do so we can reverse a string and compare with the original one


public class Test {
    public static void main(String[] args) {
        String str = "Tech Wealth Buzz";
        if (isPalindrome(str)) {
            System.out.println(str + " Is a palindrome.");
        } else {
            System.out.println(str + " Is not a palindrome.");
        }
    }

    public static boolean isPalindrome(String str) {
        str = str.replaceAll("\\s", "").toLowerCase();
        String reversed = new StringBuilder(str).reverse().toString();
        return str.equals(reversed);
    }
}


3) Count the occurances of numeric digits in the string


public class Test {
    public static void main(String[] args) {
        String str = "Tech 124 Wealth 323 Buzz 672";
        int count = countOccurrence(str);
        System.out.println("Number of digits in the string: " + count);
    }

    public static int countOccurrence(String str) {
        int count = 0;
        for (int i = 0; i < str.length(); i++) {
            char ch = str.charAt(i);
            if (Character.isDigit(ch)) {
                count++;
            }
        }
        return count;
    }
}


4) Find if two given strings are anagrams


public class Test {
    public static void main(String[] args) {
        String str1 = "Tech Wealth Buzz";
        String str2 = "Wealth Tech Buzz";
        if (areAnagrams(str1, str2)) {
            System.out.println(str1 + " and " + str2 + " are anagrams.");
        } else {
            System.out.println(str1 + " and " + str2 + " are not anagrams.");
        }
    }

    public static boolean areAnagrams(String str1, String str2) {
        str1 = str1.replaceAll("\\s", "").toLowerCase();
        str2 = str2.replaceAll("\\s", "").toLowerCase();

        if (str1.length() != str2.length()) {
            return false;
        }
        char[] charArray1 = str1.toCharArray();
        char[] charArray2 = str2.toCharArray();
        Arrays.sort(charArray1);
        Arrays.sort(charArray2);
        return Arrays.equals(charArray1, charArray2);
    }
}


5) How to reverse an Array


public class Test {
    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 4, 5};
        System.out.println("Original Array:");
        System.out.println(Arrays.toString(arr));
        reverse(arr);
        System.out.println("Reversed Array:");
        System.out.println(Arrays.toString(arr));
    }
    public static void reverse(int[] arr) {
        int start = 0;
        int end = arr.length - 1;
        while (start < end) {
            int temp = arr[start];
            arr[start] = arr[end];
            arr[end] = temp;
            start++;
            end--;
        }
    }
}