Top 20 Java Coding Interview Questions with Real Solutions – 2025 Edition
Here are 20 hand-picked Java coding problems frequently asked in interviews (TCS, Infosys, Capgemini, etc.) for 1–3 years experience. Each includes a brief explanation and clean code solution.
1. Reverse a String
public String reverse(String input) {
return new StringBuilder(input).reverse().toString();
}
2. Check if a String is Palindrome
public boolean isPalindrome(String str) {
int i = 0, j = str.length() - 1;
while (i < j) {
if (str.charAt(i++) != str.charAt(j--)) return false;
}
return true;
}
3. Find Duplicates in Array
Set set = new HashSet<>();
for (int num : arr) {
if (!set.add(num)) System.out.println("Duplicate: " + num);
}
4. Check if Two Strings Are Anagrams
public boolean isAnagram(String s1, String s2) {
char[] a1 = s1.toCharArray(), a2 = s2.toCharArray();
Arrays.sort(a1); Arrays.sort(a2);
return Arrays.equals(a1, a2);
}
5. Frequency of Characters in String
Map map = new HashMap<>();
for (char c : str.toCharArray()) {
map.put(c, map.getOrDefault(c, 0) + 1);
}
6. First Non-Repeating Character
Map freq = new LinkedHashMap<>();
for (char c : str.toCharArray()) freq.put(c, freq.getOrDefault(c, 0) + 1);
for (Map.Entry entry : freq.entrySet()) {
if (entry.getValue() == 1) return entry.getKey();
}
7. Remove Duplicates from List
List distinct = list.stream().distinct().collect(Collectors.toList());
8. FizzBuzz
for (int i = 1; i <= 100; i++) {
if (i % 15 == 0) System.out.println("FizzBuzz");
else if (i % 3 == 0) System.out.println("Fizz");
else if (i % 5 == 0) System.out.println("Buzz");
else System.out.println(i);
}
9. Sort HashMap by Value
map.entrySet()
.stream()
.sorted(Map.Entry.comparingByValue())
.forEach(e -> System.out.println(e.getKey() + ": " + e.getValue()));
10. Count Vowels and Consonants
int v = 0, c = 0;
for (char ch : str.toLowerCase().toCharArray()) {
if ("aeiou".indexOf(ch) != -1) v++;
else if (Character.isLetter(ch)) c++;
}
11. Check Armstrong Number
int num = 153, temp = num, result = 0;
while (temp != 0) {
int d = temp % 10;
result += d * d * d;
temp /= 10;
}
System.out.println(result == num);
12. Factorial using Recursion
public int factorial(int n) {
return (n == 0) ? 1 : n * factorial(n - 1);
}
13. Binary to Decimal Conversion
int decimal = Integer.parseInt("1010", 2);
14. Prime Number Check
boolean isPrime(int n) {
if (n <= 1) return false;
for (int i = 2; i <= Math.sqrt(n); i++) {
if (n % i == 0) return false;
}
return true;
}
15. Find Second Largest Number
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;
}
}
16. Find Missing Number in Range
int sum = (n * (n + 1)) / 2;
for (int val : arr) sum -= val;
System.out.println("Missing: " + sum);
17. Sort Custom Objects with Comparator
Collections.sort(list, (a, b) -> a.getAge() - b.getAge());
18. Java 8 Map and Filter
list.stream()
.filter(s -> s.startsWith("A"))
.map(String::toUpperCase)
.forEach(System.out::println);
19. Group By Using Java Streams
Map> grouped =
employees.stream().collect(Collectors.groupingBy(Employee::getDept));
20. Read File Line by Line
Files.lines(Paths.get("file.txt"))
.forEach(System.out::println);
Pro Tip: Practice these in a local IDE, explain each step out loud (for interviews), and create variants of each to boost confidence.
📅 Last Updated: 19 May 2025
📎 #JavaCodingInterview #JavaPractice2025 #CodeWithConfidence
0 Comments