Java Interview Questions
In this page:
String Reversal
Reversing a string is commonly implemented by converting it to a char[], swapping characters from both ends inward, or simply calling new StringBuilder(str).reverse() for the built-in shortcut.
Example: String Reversal
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Declare text and set it to "hello"
String text = "hello";
// Print a line to the console
System.out.println(new StringBuilder(text).reverse());
}
}
Login to try C/C++/Java/PHP code in the editor
Palindrome Check
A palindrome check compares a string against its own reverse, or walks inward from both ends simultaneously comparing characters — the same underlying idea just implemented two different ways.
Example: Palindrome Check
// Define the class Main
public class Main {
static boolean isPalindrome(String s) {
return s.equals(new StringBuilder(s).reverse().toString());
}
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Print a line to the console
System.out.println(isPalindrome("racecar"));
}
}
Login to try C/C++/Java/PHP code in the editor
Finding Duplicates
Finding duplicates in a collection is typically solved by tracking seen elements in a HashSet: if an element is already present when you try to add it, you've found a duplicate.
Example: Finding Duplicates
// Import java.util.HashSet so it can be used by its short name
import java.util.HashSet;
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Declare nums and set it to {1, 2, 3, 2}
int[] nums = {1, 2, 3, 2};
// Create a new HashSet object and store it in seen
HashSet<Integer> seen = new HashSet<>();
for (int n : nums) {
// Check whether !seen.add(n)
if (!seen.add(n)) {
// Print a line to the console
System.out.println("Duplicate: " + n);
}
}
}
}
Login to try C/C++/Java/PHP code in the editor
The FizzBuzz Problem
FizzBuzz — printing Fizz for multiples of 3, Buzz for multiples of 5, and FizzBuzz for multiples of both — is a classic beginner interview problem testing basic control flow and the modulo operator.
Example: The FizzBuzz Problem
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Loop: repeat while the condition holds
for (int i = 1; i <= 15; 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);
}
}
}
Login to try C/C++/Java/PHP code in the editor
Counting Character Frequencies
Counting character frequencies usually uses a HashMap<Character, Integer>, incrementing each character's count as you scan through the string once.
Example: Counting Character Frequencies
// Import java.util.HashMap so it can be used by its short name
import java.util.HashMap;
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Declare text and set it to "hello"
String text = "hello";
// Create a new HashMap object and store it in freq
HashMap<Character, Integer> freq = new HashMap<>();
for (char c : text.toCharArray()) {
freq.merge(c, 1, Integer::sum);
}
// Print a line to the console
System.out.println(freq);
}
}
Login to try C/C++/Java/PHP code in the editor
- Reversing a string with
+=in a loop, whenStringBuilder.reverse()is simpler. - Checking a palindrome without ignoring case and spaces.
- Checking FizzBuzz for 3 and 5 before 15, so multiples of 15 print only
Fizzinstead ofFizzBuzz.
- Utility classes such as
Math, date and time, andRandomsupport common tasks. - Enums, annotations, and scope and lifetime describe more of the language.
- Best practices, common mistakes, and interview questions help you apply what you learned.
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: