Java Notes
30+ Java coding interview questions with solutions covering strings, arrays, patterns, recursion, and data structure problems commonly asked in placements.
1. Reverse a String
3. Find Duplicate Characters in String
public Map<Character, Integer> findDuplicates(String str) {
Map<Character, Integer> map = new LinkedHashMap<>();
for (char c : str.toCharArray()) {
map.put(c, map.getOrDefault(c, 0) + 1);
}
map.entrySet().removeIf(e -> e.getValue() == 1);
return map;
}
// "programming" → {r=2, g=2, m=2}"programming" → {r=2, g=2, m=2}4. Fibonacci Series
0 1 1 2 3 5 8 13 21 34
5. Check if Number is Prime
6. Find Second Largest Element in Array
7. Two Sum Problem
8. Remove Duplicates from Array
9. Reverse a Linked List
public ListNode reverseList(ListNode head) {
ListNode prev = null, current = head;
while (current != null) {
ListNode next = current.next;
current.next = prev;
prev = current;
current = next;
}
return prev;
}10. Check Balanced Parentheses
"{[()]}" → true, "{[(])}" → false11. Find Missing Number in Array (1 to N)
12. String Anagram Check
public boolean isAnagram(String s1, String s2) {
if (s1.length() != s2.length()) return false;
char[] a = s1.toLowerCase().toCharArray();
char[] b = s2.toLowerCase().toCharArray();
Arrays.sort(a);
Arrays.sort(b);
return Arrays.equals(a, b);
}
// "listen", "silent" → true13. Binary Search
14. Find First Non-Repeating Character
public char firstNonRepeating(String str) {
LinkedHashMap<Character, Integer> map = new LinkedHashMap<>();
for (char c : str.toCharArray()) {
map.put(c, map.getOrDefault(c, 0) + 1);
}
for (Map.Entry<Character, Integer> entry : map.entrySet()) {
if (entry.getValue() == 1) return entry.getKey();
}
return '\0';
}
// "aabbcdd" → 'c'"aabbcdd" → 'c'
15. Merge Two Sorted Arrays
16. Power of Two Check
public class PowerOfTwo {
public static boolean isPowerOfTwo(int n) {
return n > 0 && (n & (n - 1)) == 0;
}
public static void main(String[] args) {
System.out.println(isPowerOfTwo(16));
System.out.println(isPowerOfTwo(18));
System.out.println(isPowerOfTwo(1024));
}
}true false true
17. Count Vowels and Consonants
Vowels: 3 Consonants: 7
18. Factorial (Recursive + Iterative)
5! (recursive) = 120 10! (iterative) = 3628800
19. GCD using Euclidean Algorithm
public class GCD {
public static int gcd(int a, int b) {
return b == 0 ? a : gcd(b, a % b);
}
public static void main(String[] args) {
System.out.println("GCD(48, 18) = " + gcd(48, 18));
System.out.println("GCD(56, 98) = " + gcd(56, 98));
}
}GCD(48, 18) = 6 GCD(56, 98) = 14
20. Rotate Array by K Positions
[5, 6, 7, 1, 2, 3, 4]
21. Maximum Subarray Sum (Kadane's Algorithm)
Max subarray sum: 6
22. Detect Cycle in Linked List (Floyd's Algorithm)
public class LinkedListCycle {
static class ListNode {
int val;
ListNode next;
ListNode(int val) { this.val = val; }
}
public static boolean hasCycle(ListNode head) {
ListNode slow = head, fast = head;
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
if (slow == fast) return true;
}
return false;
}
public static void main(String[] args) {
ListNode head = new ListNode(1);
head.next = new ListNode(2);
head.next.next = new ListNode(3);
head.next.next.next = head.next; // Create cycle
System.out.println("Has cycle: " + hasCycle(head));
}
}Has cycle: true
23. Stack using Two Queues
import java.util.*;
public class StackUsingQueues {
Queue<Integer> q1 = new LinkedList<>();
Queue<Integer> q2 = new LinkedList<>();
public void push(int x) {
q2.add(x);
while (!q1.isEmpty()) q2.add(q1.poll());
Queue<Integer> temp = q1; q1 = q2; q2 = temp;
}
public int pop() { return q1.poll(); }
public static void main(String[] args) {
StackUsingQueues stack = new StackUsingQueues();
stack.push(1); stack.push(2); stack.push(3);
System.out.println(stack.pop());
System.out.println(stack.pop());
}
}3 2
24. Decimal to Binary Conversion
public class DecimalToBinary {
public static String toBinary(int n) {
if (n == 0) return "0";
StringBuilder sb = new StringBuilder();
while (n > 0) {
sb.append(n % 2);
n /= 2;
}
return sb.reverse().toString();
}
public static void main(String[] args) {
System.out.println("10 → " + toBinary(10));
System.out.println("25 → " + toBinary(25));
System.out.println("Built-in: " + Integer.toBinaryString(10));
}
}10 → 1010 25 → 11001 Built-in: 1010
25. Intersection of Two Arrays
import java.util.*;
import java.util.stream.*;
public class ArrayIntersection {
public static int[] intersection(int[] a, int[] b) {
Set<Integer> setA = Arrays.stream(a).boxed().collect(Collectors.toSet());
return Arrays.stream(b).filter(setA::contains).distinct().toArray();
}
public static void main(String[] args) {
int[] a = {1, 2, 3, 4, 5};
int[] b = {3, 4, 5, 6, 7};
System.out.println(Arrays.toString(intersection(a, b)));
}
}[3, 4, 5]
26. Longest Common Prefix
Prefix: fl
27. Sort Array of 0s, 1s, 2s (Dutch National Flag)
[0, 0, 0, 1, 1, 1, 2, 2, 2]
28. Matrix Diagonal Sum
Diagonal sum: 25
29. Check Palindrome Linked List
public class PalindromeLinkedList {
static class ListNode {
int val; ListNode next;
ListNode(int val) { this.val = val; }
}
public static boolean isPalindrome(ListNode head) {
ListNode slow = head, fast = head;
while (fast != null && fast.next != null) {
slow = slow.next; fast = fast.next.next;
}
ListNode rev = reverse(slow);
while (rev != null) {
if (head.val != rev.val) return false;
head = head.next; rev = rev.next;
}
return true;
}
private static ListNode reverse(ListNode head) {
ListNode prev = null;
while (head != null) {
ListNode next = head.next;
head.next = prev; prev = head; head = next;
}
return prev;
}
public static void main(String[] args) {
ListNode head = new ListNode(1);
head.next = new ListNode(2);
head.next.next = new ListNode(2);
head.next.next.next = new ListNode(1);
System.out.println("Is palindrome: " + isPalindrome(head));
}
}Is palindrome: true
30. LRU Cache Implementation
import java.util.*;
public class LRUCache extends LinkedHashMap<Integer, Integer> {
private int capacity;
public LRUCache(int capacity) {
super(capacity, 0.75f, true); // access-order = true
this.capacity = capacity;
}
@Override
protected boolean removeEldestEntry(Map.Entry<Integer, Integer> eldest) {
return size() > capacity;
}
public static void main(String[] args) {
LRUCache cache = new LRUCache(3);
cache.put(1, 10); cache.put(2, 20); cache.put(3, 30);
cache.get(1); // Access 1 - makes it recently used
cache.put(4, 40); // Evicts key 2 (least recently used)
System.out.println("Contains 2: " + cache.containsKey(2));
System.out.println("Contains 1: " + cache.containsKey(1));
System.out.println("Cache: " + cache);
}
}Contains 2: false
Contains 1: true
Cache: {3=30, 1=10, 4=40}Summary
In this chapter, we learned about Coding Interview Questions in detail. Here are the key points:
- Basic introduction to the concept and why it is needed
- Syntax and structure with complete examples
- Internal working and best practices
- Real-world applications and use cases
- Common mistakes and how to avoid them
- How this topic is asked in interviews
To practice these concepts, write and run the code in your IDE and verify the output. Modify each example and experiment so that you deeply understand the concept.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Coding Interview Questions.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Java Master Course topic.
Search Terms
java-master-course, java master course, java, master, course, interview, questions, coding
Related Java Master Course Topics