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
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
17. Count Vowels and Consonants
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
18. Factorial (Recursive + Iterative)
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
19. GCD using Euclidean Algorithm
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
20. Rotate Array by K Positions
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
21. Maximum Subarray Sum (Kadane's Algorithm)
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
22. Detect Cycle in Linked List (Floyd's Algorithm)
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
23. Stack using Two Queues
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
24. Decimal to Binary Conversion
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
25. Intersection of Two Arrays
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
26. Longest Common Prefix
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
27. Sort Array of 0s, 1s, 2s (Dutch National Flag)
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
28. Matrix Diagonal Sum
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
29. Check Palindrome Linked List
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
30. LRU Cache Implementation
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
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.
Continue the lesson path