← Back to Core Java Course | Chapter 13: Advanced Topics & Reference | Lesson 10 of 10

Java Interview Questions

Interview questions are classic puzzles asked when applying for programming jobs, like reversing a word or FizzBuzz. Practicing them is like rehearsing before a big game.

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

java
// 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());
	}
}

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

java
// 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"));
	}
}

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

java
// 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);
			}
		}
	}
}

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

java
// 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);
		}
	}
}

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

java
// 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);
	}
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Reversing a string with += in a loop, when StringBuilder.reverse() is simpler.
  2. Checking a palindrome without ignoring case and spaces.
  3. Checking FizzBuzz for 3 and 5 before 15, so multiples of 15 print only Fizz instead of FizzBuzz.
Chapter Summary
  • Utility classes such as Math, date and time, and Random support 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.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.