Java Interview Questions - Part 1: All About Strings
Welcome to our new series: Java Interview Questions - Part 1. If you're preparing for your next IT or Software Developer interview, especially for Java roles, this post is just for you. Today, we’re going to talk about one of the favourite topics of interviewers — Strings in Java. 😄
We’ll keep the tone light and explanations simple. Just like how you would explain it to your friend during a late-night study session.
Whether you are a fresher or someone with a couple of years of experience, these string-based questions are often asked in companies like TCS, Infosys, Accenture and so on. So let’s get into it. 💻☕
1. Reverse a String / How to reverse a string in Java?
String input = "hello";
StringBuilder sb = new StringBuilder(input);
System.out.println(sb.reverse());
Output: olleh
Output: olleh
Why StringBuilder instead of StringBuffer?
You might wonder why we used StringBuilder here and not StringBuffer. Both help in creating mutable strings (strings that can change), but there’s a key difference:
- StringBuffer is thread-safe (synchronized), meaning it can safely be used by multiple threads at once.
- StringBuilder is not thread-safe, but faster because it doesn’t have the overhead of synchronization.
2. Check if a String is a Palindrome
String input = "madam";
StringBuffer sb = new StringBuffer(input);
StringBuffer sb = new StringBuffer(input);
System.out.println(s.length()>=3 && s.equalsIgnoreCase(sb.reverse().toString()));
Output: true
3. Find Duplicate Characters in a String
Output: true
3. Find Duplicate Characters in a String
String input = "programming";
Map<Character,Integer> map = new HashMap<>();
for(int i=0; i< input.length(); i++) {char c = input.charAt(i);map.put(c, map.getOrDefault(c, 0)+1);
}
for(Entry<Character,Integer> entry: map.entrySet()) {
if(entry.getValue()>1) {System.out.println(entry.getKey());}}
Output: r,g,m
String input = "programming";
Map<Character,Integer> map = new HashMap<>();
for(int i=0; i< input.length(); i++) {
Map<Character,Integer> map = new HashMap<>();
for(int i=0; i< input.length(); i++) {
char c = input.charAt(i);
map.put(c, map.getOrDefault(c, 0)+1);
}
for(Entry<Character,Integer> entry: map.entrySet()) {
if(entry.getValue()>1) {System.out.println(entry.getKey());}}
Output: r,g,m
}
for(Entry<Character,Integer> entry: map.entrySet()) {
if(entry.getValue()>1) {System.out.println(entry.getKey());}}
Output: r,g,m
4. Remove All Whitespaces
String input = " Java Interview ";
String result = input.replaceAll("\\s", "");
System.out.println(result);
Output: JavaInterview
Output: JavaInterview
5. First Non-Repeated Character
String input = "swiss";
LinkedHashMap<Character, Integer> map = new LinkedHashMap<>();
for (char c : input.toCharArray()) {
map.put(c, map.getOrDefault(c, 0) + 1);
}
for (Map.Entry<Character, Integer> entry : map.entrySet()) {
if (entry.getValue() == 1) {
System.out.println("First non-repeating character: " + entry.getKey());
break;
}
}
Output: w
6. Check if Two Strings are Anagrams
String a = "listen", b = "silent";
char[] arr1 = a.toCharArray();
char[] arr2 = b.toCharArray();
Arrays.sort(arr1);
Arrays.sort(arr2);
System.out.println(Arrays.equals(arr1, arr2));
Output: true
7. Count Occurrences of a Character
String s = "banana";
char c = 'a';
HashMap<Character, Integer> map = new HashMap<>();
char c = 'a';
HashMap<Character, Integer> map = new HashMap<>();
for( char c1 : s.toCharArray()) {
map.put(c1, map.getOrDefault(c1, 0)+1);}
for(Entry<Character, Integer> entry : map.entrySet()) {
if(entry.getKey().equals(c)) {
System.out.println(entry.getValue());}}
Output: 3
8. Convert String to Integer(without using built-in methods)
String str = "123";
int result = 0;
for (char c : str.toCharArray()) {
result = result * 10 + Character.getNumericValue(c);
}
System.out.println(result);
Output: 123
Output: 123
9. Check if One String Contains Another
String big = "Hello India";
String small = "India";
System.out.println(big.contains(small));
Output: true
10. Replace All Occurrences of a Character
String input = "banana";
String output = input.replace('a', '@');
System.out.println(output);
Output: b@n@n@
Output: b@n@n@
11. Split a String by Delimiter
String input = "apple,banana,grapes";
String[] fruits = input.split(",");
for(String fruit: fruits) {
System.out.println(fruit);}
Output:
apple
banana
grapes
Output:
apple
banana
grapes
12. Sort Characters in a String Alphabetically
String input = "java";
char[] arr = input.toCharArray();
Arrays.sort(arr);
System.out.println(new String(arr));
Output: aajv
13. Find the Longest Word in a Sentence
String sentence = "Java is a beautiful language";
String[] words = sentence.split(" ");
if (words.length == 0 || (words.length == 1 && words[0].isEmpty())) {
System.out.println(""); }
String longestWord = "";
for (String word : words) {
if (word.length() > longestWord.length()) {
longestWord = word; }}
System.out.println(longestWord);
Output: beautiful
14. Find Repeated Words in a String
String input = "Java is great and Java is powerful";
String[] words = input.toLowerCase().split(" ");
Map<String, Integer> wordCount = new HashMap<>();
for (String word : words) {
wordCount.put(word, wordCount.getOrDefault(word, 0) + 1);
}
for(Entry<String, Integer> entry: wordCount.entrySet()) {
if(entry.getValue()>1) {
System.out.println(entry.getKey() + " - " + entry.getValue() + " times ");
}
}
Output:
java - 2 times
is - 2 times
Final Thoughts
Practicing string problems is one of the easiest ways to become confident in Java. These types of questions are favourites in both coding rounds and technical interviews.
Pro Tip: Try rewriting these solutions using Java 8 Streams for better practice!
If you found this helpful, please share this article with your friends, classmates or teammates who are also preparing for interviews. 💬📤
Till then, happy coding! 💻🌟