👉If you are not a Member — Read for free here :

1. Reverse a String

This is one of the most classic questions in programming interviews. It looks simple, but interviewers use it to see how clearly you think.

The task is simple: given a string, return the reversed version of it.

For example:

Input:  "java"
Output: "avaj"

A basic solution is to iterate through the string from the end and build a new string.

Example in Java:

public static String reverse(String str) {
    String result = "";
    
    for (int i = str.length() - 1; i >= 0; i--) {
        result += str.charAt(i);
    }

return result;
}

While this works, experienced developers often mention that StringBuilder is more efficient for repeated string operations. Interviewers like when candidates discuss such improvements.

This question is not really about reversing a string. It is about understanding loops, indexing, and Java string behavior.

2. Check if a String is a Palindrome

A palindrome is a word that reads the same forward and backward.

Examples:

  • madam
  • racecar
  • level

The goal is to check whether a string remains the same when reversed.

One approach is to compare characters from the beginning and the end of the string.

Example:

public static boolean isPalindrome(String str) {

int left = 0;
    int right = str.length() - 1;
    while (left < right) {
        if (str.charAt(left) != str.charAt(right)) {
            return false;
        }
        left++;
        right--;
    }
    return true;
}

This question checks whether you can think about two-pointer logic, which is very common in algorithm design.

3. Find the Largest Number in an Array

Arrays appear in almost every programming language, so interviewers often ask questions around them.

The task is simple: given an array of numbers, find the largest one.

Example:

Input:  [5, 12, 8, 20, 3]
Output: 20

Java solution:

public static int findMax(int[] arr) {

int max = arr[0];
    for (int i = 1; i < arr.length; i++) {
        if (arr[i] > max) {
            max = arr[i];
        }
    }
    return max;
}

This question helps interviewers see whether you understand array traversal and comparisons.

It is basic, but many candidates make mistakes when they rush.

4. Count the Occurrence of Each Character

This problem checks your understanding of HashMap, one of the most important data structures in Java.

Example:

Input:  "banana"
Output:
b = 1
a = 3
n = 2

Java implementation:

public static Map<Character, Integer> countCharacters(String str) {

Map<Character, Integer> map = new HashMap<>();
    for (char c : str.toCharArray()) {
        map.put(c, map.getOrDefault(c, 0) + 1);
    }
    return map;
}

This question shows whether you know how to use maps, loops, and collections effectively.

Many real applications use similar logic for counting data or grouping values.

5. Remove Duplicate Elements from an Array

Duplicate removal is another very common question.

Example:

Input:  [1,2,2,3,4,4,5]
Output: [1,2,3,4,5]

One easy approach is using a Set, because sets automatically remove duplicates.

Example:

public static Set<Integer> removeDuplicates(int[] arr) {

Set<Integer> set = new HashSet<>();
    for (int num : arr) {
        set.add(num);
    }
    return set;
}

Interviewers may ask follow-up questions such as:

  • Can you keep the original order?
  • Can you do it without extra space?

Those follow-ups help them understand your deeper thinking.

6. Find the Second Largest Number in an Array

This question is slightly more interesting than finding the largest number.

Example:

Input:  [10, 4, 8, 20, 15]
Output: 15

Java approach:

public static int secondLargest(int[] arr) {

int first = Integer.MIN_VALUE;
    int second = Integer.MIN_VALUE;
    for (int num : arr) {
        if (num > first) {
            second = first;
            first = num;
        }
        else if (num > second && num != first) {
            second = num;
        }
    }
    return second;
}

This question checks how well you manage multiple conditions and edge cases.

7. Check if Two Strings Are Anagrams

Two strings are anagrams if they contain the same characters but in a different order.

Example:

listen
silent

One common approach is sorting both strings and comparing them.

Example:

public static boolean areAnagrams(String s1, String s2) {

char[] arr1 = s1.toCharArray();
    char[] arr2 = s2.toCharArray();
    Arrays.sort(arr1);
    Arrays.sort(arr2);
    return Arrays.equals(arr1, arr2);
}

This question checks understanding of arrays, sorting, and string manipulation.

8. Fibonacci Sequence

The Fibonacci sequence is a classic algorithm problem.

Sequence example:

0, 1, 1, 2, 3, 5, 8, 13...

Each number is the sum of the previous two numbers.

Java implementation:

public static void fibonacci(int n) {

int a = 0;
    int b = 1;
    for (int i = 0; i < n; i++) {
        System.out.print(a + " ");
        int next = a + b;
        a = b;
        b = next;
    }
}

Interviewers use this question to check loop logic and sequence understanding.

9. Find Missing Number in an Array

This is a popular problem in many coding interviews.

Example:

Input:  [1,2,3,5]
Output: 4

One efficient solution uses the formula for the sum of natural numbers.

Expected sum:

n * (n + 1) / 2

Java implementation:

public static int findMissing(int[] arr, int n) {

int expectedSum = n * (n + 1) / 2;
    int actualSum = 0;
    for (int num : arr) {
        actualSum += num;
    }
    return expectedSum - actualSum;
}

This question tests whether you can combine math with programming logic.

A message from our Founder

Hey, Sunil here. I wanted to take a moment to thank you for reading until the end and for being a part of this community. Did you know that our team run these publications as a volunteer effort to over 3.5m monthly readers? We don't receive any funding, we do this to support the community.

If you want to show some love, please take a moment to follow me on LinkedIn, TikTok, Instagram. You can also subscribe to our weekly newsletter. And before you go, don't forget to clap and follow the writer️!