0% found this document useful (0 votes)
2 views6 pages

Java Interview

The document outlines various Java programming concepts including differences between data structures like HashMap and ConcurrentHashMap, memory management in Java, and the use of functional interfaces and lambda expressions. It also covers coding challenges such as implementing an LRU Cache, merging sorted linked lists, and serializing/deserializing a binary tree. Additionally, it discusses thread safety, garbage collection, and the Java Memory Model.

Uploaded by

Gopi Krishna
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views6 pages

Java Interview

The document outlines various Java programming concepts including differences between data structures like HashMap and ConcurrentHashMap, memory management in Java, and the use of functional interfaces and lambda expressions. It also covers coding challenges such as implementing an LRU Cache, merging sorted linked lists, and serializing/deserializing a binary tree. Additionally, it discusses thread safety, garbage collection, and the Java Memory Model.

Uploaded by

Gopi Krishna
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

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;

}
}

You might also like