Introduction
When interviewing Java developers, asking theoretical questions alone is rarely enough. The best interviews combine:
- Logical reasoning
- Problem-solving ability
- Understanding of loops and conditionals
- Character and string manipulation
- Collections knowledge
- Java 8 features
- Complexity analysis
This article contains 10 interview exercises commonly used to evaluate candidates ranging from 2 to 10+ years of experience.
For each question we provide:
- Problem Statement
- Sample Input/Output
- Solution
- Logic Explanation
- Time Complexity
- Space Complexity
Question 1: Reverse a String Without Using StringBuilder.reverse()
Problem
Given:
HELLO
Output:
OLLEH
Solution
public static String reverse(String input) {
char[] chars = input.toCharArray();
int left = 0;
int right = chars.length - 1;
while(left < right) {
char temp = chars[left];
chars[left] = chars[right];
chars[right] = temp;
left++;
right--;
}
return new String(chars);
}
Logic
Swap first and last characters.
Continue moving toward center.
HELLO
H <-> O
E <-> L
OLLEH
Complexity
Time:
O(n)
Space:
O(n)
Question 2: Check Palindrome
Problem
Determine whether a string reads the same forward and backward.
Input:
MADAM
Output:
TRUE
Solution
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;
}
Logic
Compare:
M == M
A == A
If all match, it is a palindrome.
Complexity
Time:
O(n)
Space:
O(1)
Question 3: Find First Non-Repeating Character
Problem
Input:
swiss
Output:
w
Solution
public static Character firstUnique(String str) {
Map<Character,Integer> map =
new LinkedHashMap<>();
for(char ch : str.toCharArray()) {
map.put(ch,
map.getOrDefault(ch,0)+1);
}
for(Map.Entry<Character,Integer> entry
: map.entrySet()) {
if(entry.getValue() == 1) {
return entry.getKey();
}
}
return null;
}
Logic
Count frequency.
Return first character whose count is 1.
Complexity
Time:
O(n)
Space:
O(n)
Question 4: Print Reverse Pyramid
Problem
Input:
5
Output:
* * * * *
* * * *
* * *
* *
*
Solution
for(int row=5; row>=1; row--) {
for(int space=0;
space<5-row;
space++) {
System.out.print(" ");
}
for(int star=1;
star<=row;
star++) {
System.out.print("* ");
}
System.out.println();
}
Logic
Two loops:
- Leading spaces
- Stars
This evaluates nested loop understanding.
Complexity
Time:
O(n²)
Space:
O(1)
Question 5: Count Character Frequency
Problem
Input:
banana
Output:
b=1
a=3
n=2
Solution
Map<Character,Integer> frequency =
new HashMap<>();
for(char ch : input.toCharArray()) {
frequency.put(
ch,
frequency.getOrDefault(ch,0)+1);
}
Logic
Maintain running count.
Complexity
Time:
O(n)
Space:
O(k)
Where k = distinct characters.
Question 6: Find Missing Number
Problem
Array contains numbers from:
1..N
One number missing.
Input:
1 2 3 5
Output:
4
Solution
public static int findMissing(int[] arr,int n){
int expected =
n * (n + 1) / 2;
int actual = 0;
for(int num : arr){
actual += num;
}
return expected - actual;
}
Logic
Use arithmetic progression formula.
Expected:
1+2+3+4+5 = 15
Actual:
11
Missing:
4
Complexity
Time:
O(n)
Space:
O(1)
Question 7: Find Duplicate Characters
Problem
Input:
programming
Output:
r
g
m
Solution
Set<Character> seen =
new HashSet<>();
Set<Character> duplicates =
new HashSet<>();
for(char ch : input.toCharArray()) {
if(!seen.add(ch)) {
duplicates.add(ch);
}
}
Logic
If character already exists in set:
Duplicate found
Complexity
Time:
O(n)
Space:
O(n)
Question 8: Sort Employees by Salary Using Java 8
Problem
Given:
Employee(id,name,salary)
Sort descending by salary.
Solution
employees.stream()
.sorted(
Comparator.comparing(
Employee::getSalary)
.reversed())
.forEach(System.out::println);
Logic
Tests:
- Streams
- Comparator
- Method references
Complexity
Time:
O(n log n)
Space:
O(n)
Question 9: Group Employees by Department
Problem
Input:
IT
IT
HR
Finance
Output:
IT -> 2
HR -> 1
Finance -> 1
Solution
Map<String,Long> result =
employees.stream()
.collect(
Collectors.groupingBy(
Employee::getDepartment,
Collectors.counting()));
Logic
Tests candidate’s Java 8 grouping skills.
Complexity
Time:
O(n)
Space:
O(n)
Question 10: Find Second Highest Number
Problem
Input:
10 20 30 40 50
Output:
40
Traditional Solution
int highest = Integer.MIN_VALUE;
int second = Integer.MIN_VALUE;
for(int num : arr) {
if(num > highest) {
second = highest;
highest = num;
} else if(num > second &&
num != highest) {
second = num;
}
}
Java 8 Solution
Integer secondHighest =
Arrays.stream(arr)
.boxed()
.distinct()
.sorted(
Comparator.reverseOrder())
.skip(1)
.findFirst()
.orElse(null);
Logic
Sort descending.
Skip largest.
Take next.
Complexity
Time:
O(n log n)
Traditional approach:
O(n)
Preferred for large datasets.
Interview Evaluation Matrix
| Question | Skill Evaluated |
|---|---|
| Reverse String | Loops, Arrays |
| Palindrome | Logic |
| First Non-Repeating | Maps |
| Reverse Pyramid | Nested Loops |
| Frequency Count | HashMap |
| Missing Number | Mathematical Thinking |
| Duplicate Characters | Set |
| Employee Sort | Java 8 Streams |
| Group By Department | Collectors |
| Second Highest | Optimization & Streams |
Conclusion
These 10 exercises cover nearly every foundational skill expected from a Java developer:
- Looping constructs
- Conditional logic
- Character manipulation
- Collections Framework
- HashMap and Set usage
- Algorithmic thinking
- Time and Space Complexity
- Java 8 Streams and Collectors
A candidate who can confidently solve and explain these problems, along with complexity analysis, is typically ready for enterprise Java and Spring Boot development projects.