Java Interview Question - New FAQs
1. Performance Testing & Setting up benchmarks
Performance testing evaluate how a system performs under load & to setup benchmarks below steps to be noted down.
Steps are:
- Define KPIs: Response time, throughput, CPU/memory usage etc.
- Identify Critical Scenarios: like login, search, submit form
- Use Tools: Gatling, Apache Bench, JMeter (Use to simulate 1000 concurrent users hitting a Rest API and measure the response time
- Baseline Measurement: Run tests under normal load to establish a baseline
- Load Testing: Gradually increase users to find system limits
- Stress Testing: Push beyond limits to see how the system fails
- Bench marking: Compare results against industry standards or previous releases
2. ACID properties in database
It ensure the reliable transaction
- Atomicity: All operations in a transaction succeed or none do
- Consistency: DB remains in a valid state before and after the transaction
- Isolation: Concurrent transactions don't interfere
- Durability: Once commited in DB, changes persist even after crashes
e.g.: JDBC Java code snippet:
Connection conn = DriverManager.getConnection(DBurl);
conn.setAutoCommit(false);
try {
conn.commit(); //Ensure Atomicity and Durability
} catch(Exception e) {
conn.rollback(); //Ensure Consistency
}
3. Deadlock in Database & How to avoid it?
Deadlock occur when two transactions wait on each other to release the locks.
Avoidance Criteria:
- Lock ordering: Always acquire locks in a consistent order
- Timeouts: Abort transaction that wait too long
- Minimize lock scope: Keep transactions short
- Use optimistic locking: Avoid locking unless necessary (e.g. use @Version annotation using Hibernate)
4. API Gateway timeout it 10 second & Micro Services response is 15 seconds (Scenario)
Below solutions can be tried out:
- ASync Processing: Return a 202 Accepted and process in background
- Queueing: Use Kafka / RabbitMQ to decouple request handling
- Caching: Serve cached responses for repeated requests
- Optimize Microservices: Profile and reduce processing time
e.g.: Using Spring Boot with async annotation
@Async
public CompletableFuture<Response> processRequest() {
//Write your long running task logic here
}
5. Explain DB Connection Pooling & Implementation
Connection pooling basically reuses DB connection to reduce overhead.
Benefits:
- Faster response
- Reduced resource usage
- Better scalability
Implementation:
Use jars libraries like Apache DBCP2, HikariCP, C3P0
6. SOLID design principles
S: Single Responsibility
class ReportGenerate {
void print(Report r) {
//Your logic here
}
}
O: Open/Closed
abstract class Shape {
abstract double area();
}
class Square extends Shape {
//Override area logic here
}
L: Liskov Substitution
Sub classes should be substitutable for base classes
I: Interface Segregation
Split large interfaces into smaller ones
D: Dependency Inversion
Depend on abstractions, not concrete classes
7. Singleton design patterns with and without Synchronized block
Without Synchronized (Not a thread-safe)
class Singleton {
private static Singleton myInstance;
public static Singleton getInstance() {
if(myInstance == null) {
myInstance = new Singleton();
}
return myInstance;
}
}
Without Synchronized (Thread-safe)
class Singleton {
private static Singleton myInstance;
public static synchronized Singleton getInstance() {
if(myInstance == null) {
myInstance = new Singleton();
}
return myInstance;
}
}
Best practice: To use double checked locking:
class Singleton {
private static volatile Singleton myInstance;
public static Singleton getInstance() {
if(myInstance == null) {
synchronized(Singleton.class) {
if(myInstance == null) {
myInstance = new Singleton();
}
}
}
return myInstance;
}
}
8. Implement Linked List in Java (Write a Program)
class Node {
int data;
Node next;
Node(int data) {
this.data = data;
this.next = null;
}
}
class LinkedList {
Node head;
void insert(int data) {
Node newNode = new Node(data);
if(head == null) {
head = newNode;
}
else {
Node temp = head;
while(temp.next != null) {
temp = temp.next;
temp.next = newNode;
}
}
}
void display() {
Node temp = head;
while(temp != null) {
System.out.print(temp.data + "-> ");
temp = temp.next;
}
System.out.println("null");
}
}
Comments
Post a Comment