Top Java Coding Round Questions Asked in Interviews (With Detailed Solutions)

Top Java Coding Round Questions Asked in Interviews (With Detailed Solutions)

Java coding rounds are a crucial part of placement drives and technical interviews conducted by companies such as TCS, Infosys, Capgemini, Wipro, Cognizant, and many others. To help you prepare effectively, here is a well-structured collection of commonly asked Java programming questions along with clear, beginner-friendly solutions.

Each problem includes:

  • A sample input/output
  • Java code solution
  • Explanation
  • Time & space complexity
  • Interview tip (What interviewers expect)

🧠 1. Reverse Each Word in a Sentence

Asked In: Infosys, Wipro

Input: "Java is awesome"
Output: "avaJ si emosewa"
public class ReverseWords {
    public static void main(String[] args) {
        String input = "Java is awesome";
        String[] words = input.split(" ");
        StringBuilder result = new StringBuilder();

        for (String word : words) {
            result.append(new StringBuilder(word).reverse())
                  .append(" ");
        }

        System.out.println(result.toString().trim());
    }
}

Explanation

The sentence is split into individual words. Each word is reversed using StringBuilder.reverse() and appended back.

Time Complexity: O(n)
Space Complexity: O(n)

Interview Tip: Mention why using StringBuilder is efficient for string manipulation.

🔢 2. Count Letters, Digits & Special Characters

Asked In: Capgemini

Input: "Java123@#!"
Output:
Letters = 4
Digits = 3
Specials = 3
public class CountTypes {
    public static void main(String[] args) {
        String str = "Java123@#!";
        int letters = 0, digits = 0, specials = 0;

        for (char ch : str.toCharArray()) {
            if (Character.isLetter(ch)) letters++;
            else if (Character.isDigit(ch)) digits++;
            else specials++;
        }

        System.out.println("Letters=" + letters + ", Digits=" + digits + ", Specials=" + specials);
    }
}

Explanation

The program iterates through each character and checks its type using Java's inbuilt Character class.

Time Complexity: O(n)

Interview Tip: Many candidates overlook edge cases like spaces and Unicode characters—mention this to impress.

🔁 3. Print Fibonacci Series up to N Terms

Asked In: Wipro, Cognizant

Input: 6
Output: 0 1 1 2 3 5
public class FibonacciSeries {
    public static void main(String[] args) {
        int n = 6, a = 0, b = 1;
        System.out.print(a + " " + b + " ");
        for (int i = 2; i < n; i++) {
            int c = a + b;
            System.out.print(c + " ");
            a = b;
            b = c;
        }
    }
}

Explanation

The Fibonacci sequence begins with 0 and 1. Each new number is the sum of the previous two.

Time Complexity: O(n)

Interview Tip: Be ready to explain recursion vs. iteration approach and which is more efficient.

🎯 4. Find the Second Highest Number in an Array

Asked In: TCS, Infosys

Input: [2, 10, 5, 7]
Output: 7
public class SecondLargest {
    public static void main(String[] args) {
        int[] arr = {2, 10, 5, 7};
        int first = Integer.MIN_VALUE, second = Integer.MIN_VALUE;

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

        System.out.println("Second largest: " + second);
    }
}

Explanation

The solution keeps track of both the highest and second-highest numbers in a single scan.

Time Complexity: O(n)
Space Complexity: O(1)

Interview Tip: Discuss why sorting is not optimal (O(n log n)).

✅ 5. Check if a String is Palindrome

Asked In: Cognizant, Capgemini

Input: "madam"
Output: true
public class PalindromeCheck {
    public static void main(String[] args) {
        String str = "madam";
        String reversed = new StringBuilder(str).reverse().toString();
        System.out.println(str.equals(reversed));
    }
}

Explanation

A string is a palindrome if it reads the same forward and backward.

Time Complexity: O(n)

Interview Tip: The interviewer may ask for a solution without using reverse() — be prepared.

🧩 6. Print Right-Angled Triangle Star Pattern

Asked In: TCS NQT

Input: 5
Output:
*
**
***
****
*****
public class TrianglePattern {
    public static void main(String[] args) {
        for (int i = 1; i <= 5; i++) {
            System.out.println("*".repeat(i));
        }
    }
}

Explanation

Star patterns are frequently asked to test looping logic and nested loops.

Interview Tip: Always describe the loop flow; interviewers check if you understand iteration deeply.

💡 7. Count the Frequency of Each Character

Asked In: Infosys, Wipro

Input: "apple"
Output:
a = 1
p = 2
l = 1
e = 1
import java.util.*;

public class CharFrequency {
    public static void main(String[] args) {
        String str = "apple";
        Map<Character, Integer> map = new LinkedHashMap<>();

        for (char c : str.toCharArray()) {
            map.put(c, map.getOrDefault(c, 0) + 1);
        }

        map.forEach((k, v) -> System.out.println(k + " = " + v));
    }
}

Explanation

The LinkedHashMap preserves insertion order while counting the frequency of each character.

Time Complexity: O(n)

Interview Tip: You may also be asked to sort characters by frequency — a common extension.

📌 Conclusion

These Java coding interview questions are widely asked across service-based and product-based companies. Practice writing optimized and clean code, and always be ready to explain your logic clearly. This is often more important to interviewers than the final answer itself.

Next article: Advanced Java Coding Round Problems Asked in Product Companies


❓ Frequently Asked Questions (FAQ)

1. Which Java topics are most important for coding rounds?

Strings, arrays, loops, collections, recursion, pattern printing, and basic math logic.

2. Are these questions enough for TCS NQT/Infosys/Wipro?

These cover 80% of commonly asked beginner-level coding problems.

3. Should I memorize code?

No — understand logic. Interviewers test your thought process, not memorization.