1. What is the difference between HashMap and ConcurrentHashMap?
Thread safety
Performance
Locking mechanism
Concurrent access behavior
2. Explain Java Memory Model (JMM).
Heap
Stack
Method Area
Synchronization
Visibility and happens-before relationship
3. What is the difference between String, StringBuilder, and StringBuffer?
Immutability
Thread safety
Performance
4. Explain volatile keyword in Java.
Visibility guarantee
Does not guarantee atomicity
Real-world use cases
5. Difference between synchronized and ReentrantLock.
Explicit locking
Fair locking
Interruptibility
6. What happens internally when you execute [Link]()?
Hash calculation
Bucket selection
Collision handling
Treeification in Java 8+
7. Explain equals() and hashCode() contract.
Why both should be overridden together
Impact on collections
8. What is the difference between ArrayList and LinkedList?
Internal structure
Insert/delete complexity
Search performance
9. Explain Java Streams API and its advantages.
Functional programming
Intermediate operations
Terminal operations
Parallel streams
10. What is the difference between ExecutorService and Thread?
Thread pooling
Resource management
Scalability
11. Explain Garbage Collection in Java.
Minor GC
Major GC
G1 GC
ZGC
Memory leak scenarios
12. What is Deadlock? How do you avoid it?
Causes
Detection
Prevention techniques
13. Explain Optional class in Java 8.
Avoiding NullPointerException
map()
flatMap()
orElse()
14. What are Functional Interfaces and Lambda Expressions?
Example:
@FunctionalInterface
interface Test {
void display();
}
15. Design Question:
Design a thread-safe Singleton class in Java.
Example:
public class Singleton {
private static volatile Singleton instance;
private Singleton() {}
public static Singleton getInstance() {
if (instance == null) {
synchronized ([Link]) {
if (instance == null) {
instance = new Singleton();
}
}
}
return instance;
}
}
Coding questions
1. LRU Cache (Least Recently Used Cache)
Difficulty: Hard
Problem:
Design a data structure that supports:
get(key) → Return value if exists, else -1
put(key, value) → Insert/update value
Remove least recently used item when capacity exceeds.
Expected Complexity:
O(1) for both operations
Java Solution:
import [Link].*;
class LRUCache {
private int capacity;
private LinkedHashMap<Integer, Integer> cache;
public LRUCache(int capacity) {
[Link] = capacity;
cache = new LinkedHashMap<>(capacity, 0.75f, true) {
protected boolean removeEldestEntry(
[Link]<Integer, Integer> eldest) {
return size() > capacity;
}
};
}
public int get(int key) {
return [Link](key, -1);
}
public void put(int key, int value) {
[Link](key, value);
}
}
2. Merge K Sorted Linked Lists
Difficulty: Hard
Problem:
Given K sorted linked lists, merge them into one sorted list.
Example:
1→4→5
1→3→4
2→6
Output:
1→1→2→3→4→4→5→6
Java Solution:
import [Link];
class ListNode {
int val;
ListNode next;
ListNode(int val) {
[Link] = val;
}
}
public ListNode mergeKLists(ListNode[] lists) {
PriorityQueue<ListNode> pq =
new PriorityQueue<>(
(a, b) -> [Link] - [Link]);
for (ListNode node : lists)
if (node != null)
[Link](node);
ListNode dummy =
new ListNode(0);
ListNode curr = dummy;
while (![Link]()) {
ListNode node = [Link]();
[Link] = node;
curr = [Link];
if ([Link] != null)
[Link]([Link]);
}
return [Link];
}
3. Serialize and Deserialize Binary Tree
Difficulty: Very Hard
Problem:
Convert a binary tree into a string and reconstruct the tree.
Java Solution:
class Codec {
String serialize(TreeNode root){
if(root==null)
return "null,";
return [Link] + ","
+ serialize([Link])
+ serialize([Link]);
TreeNode deserialize(
String data){
Queue<String> q =
new LinkedList<>(
[Link](
[Link](",")));
return build(q);
TreeNode build(
Queue<String> q){
String val=[Link]();
if([Link]("null"))
return null;
TreeNode node =
new TreeNode(
[Link](val));
[Link] =
build(q);
[Link] =
build(q);
return node;
}
}