Java String coding, Things you should know
Introduction:
In this guide on Java String Coding Interview Questions, we will cover, different programming questions related to string.
If you are preparing for a Java interview, one thing is absolutely non-negotiable — mastering Java String Coding Interview Questions.
Strings may look simple at first, but in reality, they are one of the most commonly asked and deeply tested topics in technical interviews. From basic operations like reversing a string to advanced problems like finding the longest substring without repeating characters, string-based questions are designed to evaluate your problem-solving ability, logic building, and optimization skills.
Most candidates fail not because they don’t know Java, but because they struggle to explain the logic clearly, handle edge cases, or optimize their solutions under pressure. Interviewers are not just looking for correct answers — they want to see how you think, how you approach a problem, and how efficiently you can solve it.
In this guide, we have covered the top 15 Java String Coding Interview Questions with:
- Clear problem descriptions
- Step-by-step logic explanations
- Multiple approaches for each problem
- Complete Java code
- Input and output examples
Whether you are a beginner or an experienced professional preparing for a role like Automation Tester, SDET, or Java Developer, this guide will help you build confidence and crack interviews with ease.
1. Reverse a String
Description:
Given a string, reverse it so that characters appear in opposite order.
Logic:
Approach 1 (Built-in)
- Take input string
- Convert it to
StringBuilder - Use
.reverse() - Convert back to string
Approach 2 (Manual)
- Start from last index
- Traverse backward
- Append each character to result
Approach 1
String input = "hello";
String result = new StringBuilder(input).reverse().toString();
System.out.println(result);
Approach 2
String input = "hello";
String result = "";for(int i = input.length() - 1; i >= 0; i--){
result += input.charAt(i);
}
System.out.println(result);
Input:
hello
Output
olleh
2. Palindrome Check
Description:
Check if a string reads same forward and backward.
Logic:
Approach 1:
- Reverse string
- Compare with original
Approach 2:
- Use two pointers (start, end)
- Compare characters
- Move inward
Code:
Approach 1
String str = "madam";
String rev = new StringBuilder(str).reverse().toString();
System.out.println(str.equals(rev));
Approach 2
String str = "madam";
int start = 0, end = str.length() - 1;
boolean isPalindrome = true;
while(start < end){
if(str.charAt(start) != str.charAt(end)){
isPalindrome = false;
break;
}
start++;
end--;
}
System.out.println(isPalindrome);
Input
madam
Output
true
3. Count Vowels and Consonants:
Description:
Count how many vowels and consonants are present.
Logic:
- Loop through string
- Check if character is vowel (
aeiou) - Else count as consonant
Code:
Approach 1
String str = "automation";
int vowels = 0, consonants = 0;
for(char c : str.toCharArray())
{
if("aeiou".indexOf(c) != -1)
vowels++;
else
consonants++;
}
System.out.println("Vowels: " + vowels);
System.out.println("Consonants: " + consonants);
Approach 2
for(char c : str.toCharArray()){
switch(c){
case 'a': case 'e': case 'i': case 'o': case 'u':
vowels++; break;
default:
consonants++;
}
}
Input
automation
Output
Vowels: 6
Consonants: 4
4. Find Duplicate Characters:
Description:
Print characters that appear more than once.
Logic:
Approach 1
- Store frequency in map
- Print characters with count > 1
Approach 2
- Track characters in set
- If already exists → duplicate
Code
Approach 1
String str = "programming";
Map<Character, Integer> map = new HashMap<>();
for(char c : str.toCharArray()){
map.put(c, map.getOrDefault(c, 0) + 1);
}
for(char c : map.keySet()){
if(map.get(c) > 1){
System.out.println(c);
}
}
Approach 2
Set<Character> seen = new HashSet<>();
Set<Character> dup = new HashSet<>();
for(char c : str.toCharArray()){
if(!seen.add(c)){
dup.add(c);
}
}
System.out.println(dup);
Input
programming
Output
r g m
5. Anagram Check
Description:
Check if two strings contain same characters in different order.
Logic:
Approach 1
Sort both strings → compare
Approach 2
Count frequency → compare
Code:
Approach 1
String s1 = "listen";
String s2 = "silent";
char[] a = s1.toCharArray();
char[] b = s2.toCharArray();
Arrays.sort(a);
Arrays.sort(b);
System.out.println(Arrays.equals(a, b));
Approach 2
int[] count = new int[26];
for(char c : s1.toCharArray()) count[c - 'a']++;
for(char c : s2.toCharArray()) count[c - 'a']--;
boolean flag = true;
for(int i : count){
if(i != 0){
flag = false;
break;
}
}
System.out.println(flag);
Input
listen, silent
Output
true
6. Remove Whitespaces
Description:
Remove all spaces from string.
Logic:
- Either use regex
- Or manually skip spaces
Code:
Approach 1
String result = str.replaceAll("\\s", "");
Approach 2
String result = "";
for(char c : str.toCharArray()){
if(c != ' ')
result += c;
}
Input
“hello world”
Output
helloworld
7. First Non-Repeated Character
Description:
Find first character that appears only once.
Logic:
Approach 1
- Store frequency
- Return first count=1
Approach 2
Compare each char with others
Code:
Approach 1
String str = "swiss";
Map<Character, Integer> map = new LinkedHashMap<>();
for(char c : str.toCharArray()){
map.put(c, map.getOrDefault(c, 0) + 1);
}
for(char c : map.keySet()){
if(map.get(c) == 1){
System.out.println(c);
break;
}
}
Approach 2
for(int i=0;i<str.length();i++){
boolean unique = true;
for(int j=0;j<str.length();j++){
if(i!=j && str.charAt(i)==str.charAt(j)){
unique = false;
break;
}
}
if(unique){
System.out.println(str.charAt(i));
break;
}
}
Input
swiss
Output
w
8. Remove Duplicate Characters
Description:
Remove repeated characters and keep only unique ones.
Logic:
- Maintain order
- Add only first occurrence
Code:
Approach 1
Set<Character> set = new LinkedHashSet<>();
for(char c : str.toCharArray()){
set.add(c);
}
System.out.println(set);
Approach 2
StringBuilder sb = new StringBuilder();
for(char c : str.toCharArray()){
if(sb.indexOf(String.valueOf(c)) == -1){
sb.append(c);
}
}
System.out.println(sb);
Input
programming
Output
progamin
9. Count Words
Description:
Count number of words in string.
Logic:
Approach 1
Split by space
Approach 2
Count spaces
Code:
Approach 1
String str = "Java is awesome";
int count = str.trim().split("\\s+").length;
System.out.println(count);
Approach 2
int count = 1;
for(int i=0;i<str.length();i++){
if(str.charAt(i) == ' ')
count++;
}
System.out.println(count);
Input
Java is awesome
Output
3
These types of Java String Coding Interview Questions are frequently asked in real interviews.
10. Check Only Digits:
Description:
Check if string contains only numbers.
Logic:
Loop and validate each character
Code:
Approach 1
System.out.println(str.matches("\\d+"));
Approach 2
boolean flag = true;
for(char c : str.toCharArray()){
if(!Character.isDigit(c)){
flag = false;
break;
}
}
System.out.println(flag);
Input
12345
Output
true
11. First Repeated Character
Description:
Find first character that appears twice.
Logic:
If set already contains → repeated
Code:
Set<Character> set = new HashSet<>();
for(char c : str.toCharArray()){
if(!set.add(c)){
System.out.println(c);
break;
}
}
Input
programming
Output
r
12. Sort Characters
Description:
Sort string alphabetically.
Logic:
Convert to array → sort → convert back
Code:
char[] arr = str.toCharArray();
Arrays.sort(arr);
System.out.println(new String(arr));
Input:
dcba
Output:
abcd
13. All Substrings
Description
Print all substrings.
Logic:
Fix start → expand end
Code:
for(int i=0;i<str.length();i++){
for(int j=i+1;j<=str.length();j++){
System.out.println(str.substring(i,j));
}
}
Input:
abc
Output:
a
ab
abc
b
bc
c
14. Longest Substring Without Repeating
Description:
Find longest substring with unique characters.
Logic:
Sliding window:
- Expand window
- Remove duplicates
- Track max length
Code:
Set<Character> set = new HashSet<>();
int max = 0, left = 0;
for(int right = 0; right < str.length(); right++){
while(set.contains(str.charAt(right))){
set.remove(str.charAt(left++));
}
set.add(str.charAt(right));
max = Math.max(max, right - left + 1);
}
System.out.println(max);
Input:
abcabcbb
Output:
3
15. String Rotation
Description:
Check if one string is rotation of another.
Logic:
If rotated → must exist in s1+s1
Code:
String s1 = "waterbottle";
String s2 = "erbottlewat";
System.out.println((s1 + s1).contains(s2));
Input:
waterbottle, erbottlewat
Output:
true
💥 FINAL TRUTH (No Drama)
If you:
- Practice these properly
- Explain logic like above
- Write both approaches
👉 You’re ahead of 90% candidates.
Mastering Java String Coding Interview Questions is not about memorizing solutions — it’s about understanding patterns, practicing consistently, and learning how to think like an interviewer.
If you have gone through all the problems in this guide, you have already built a strong foundation in string manipulation. The next step is to practice these questions without looking at the solutions, try to explain your approach out loud, and focus on writing clean, optimized code.
Remember, in interviews:
- Clarity of thought matters more than speed
- Logic matters more than syntax
- Optimization matters more than brute force
If you can confidently solve and explain these problems, you are already ahead of most candidates.
Keep practicing, stay consistent, and keep pushing your limits — because the difference between rejection and selection is often just one well-explained solution.
Practicing these Java String Coding Interview Questions regularly.
Conclusion
Mastering Java String Coding Interview Questions is not about memorizing solutions — it’s about understanding patterns, practicing consistently, and learning how to think like an interviewer. These Java String Coding Interview Questions are essential for cracking technical interviews.
If you have gone through all the problems in this guide, you have already built a strong foundation in string manipulation. The next step is to practice these questions without looking at the solutions, try to explain your approach out loud, and focus on writing clean, optimized code.
Remember, in interviews:
- Clarity of thought matters more than speed
- Logic matters more than syntax
- Optimization matters more than brute force
If you can confidently solve and explain these problems, you are already ahead of most candidates.
Keep practicing, stay consistent, and keep pushing your limits — because the difference between rejection and selection is often just one well-explained solution.
Recommended External Links
📘 Java Official Documentation (High Authority)
📘 StringBuilder (For Reverse, Optimization)
📘 Collections Framework (For Map, Set Questions)
📘
📘 Regex Guide (For Digit / Space Questions)
📘 Big-O Complexity (VERY IMPORTANT for Interviews)
Have a look on Testng related Blog TestNG Automation Framework – Complete Architect Guide for Enterprise CI/CD & Parallel Execution
Have a look on Cucumber related Blog For a complete BDD implementation guide, read our Cucumber Automation Framework – Complete Beginner to Advanced Guide.
Have a look on API Authentication related Blog , read our The Ultimate API Authentication guide
Have a look on Playwright related Blog , read our Playwright-Interview-Questions-Guide