Complete Java Interview Preparation
π― Overviewβ
This comprehensive guide covers everything from core Java concepts to system design problems commonly asked in technical interviews for Java developer positions.
π Part 1: Core Java Fundamentalsβ
1.1 Object-Oriented Programming (OOP)β
Key Concepts:
- Encapsulation: Bundling data and methods that operate on that data within a single unit (class), hiding internal state and requiring interaction through methods.
- Inheritance: Mechanism where a new class derives properties and behaviors from an existing class.
- Polymorphism: Ability of objects to take multiple forms. Includes compile-time (method overloading) and runtime (method overriding).
- Abstraction: Hiding complex implementation details and showing only essential features.
Critical Interview Questions:
- Abstract Class vs Interface: Abstract classes can have constructors, state, and method implementations. Interfaces (pre-Java 8) could only have abstract methods. Post-Java 8, interfaces can have default and static methods.
- Multiple Inheritance: Java doesn't support multiple class inheritance but achieves it through interfaces.
- Diamond Problem: Resolved using default methods in interfaces with explicit method selection.
1.2 Access Modifiersβ
| Modifier | Class | Package | Subclass | World |
|---|---|---|---|---|
| public | β | β | β | β |
| protected | β | β | β | β |
| default | β | β | β | β |
| private | β | β | β | β |
Interview Focus: Difference between protected and default (package-private).
1.3 Keywords Deep Diveβ
static:
- Belongs to class, not instance
- Static methods cannot access instance variables
- Cannot be overridden (can be hidden)
- Static blocks execute when class is loaded
final:
- Variables: Value cannot change (constant)
- Methods: Cannot be overridden
- Classes: Cannot be extended
- final vs finally vs finalize(): final is keyword, finally is block in exception handling, finalize() is method called by GC before object destruction (deprecated in Java 9+)
this vs super:
this: Reference to current objectsuper: Reference to parent class object
π§ Part 2: Java Memory & JVM Internalsβ
2.1 JVM Architectureβ
Components:
- Class Loader Subsystem: Bootstrap, Extension, Application class loaders
- Runtime Data Areas: Method Area, Heap, Stack, PC Registers, Native Method Stack
- Execution Engine: Interpreter, JIT Compiler, Garbage Collector
2.2 Memory Managementβ
Heap vs Stack:
- Heap: Stores objects and instance variables. Shared across threads. Slower access.
- Stack: Stores method calls, local variables, references. Thread-specific. Faster access.
Garbage Collection:
- Types: Minor GC (Young Generation), Major GC (Old Generation), Full GC
- Algorithms: Mark and Sweep, Generational GC
- GC Types: Serial, Parallel, CMS, G1GC, ZGC
- Reference Types: Strong, Soft, Weak, Phantom
Common Questions:
- How to prevent memory leaks? Close resources, avoid static collections holding references, use weak references when appropriate.
- When does OutOfMemoryError occur? Heap space exhaustion, PermGen/Metaspace issues.
β οΈ Part 3: Exception Handlingβ
Exception Hierarchyβ
Throwable
βββ Error (OutOfMemoryError, StackOverflowError)
βββ Exception
βββ RuntimeException (Unchecked)
β βββ NullPointerException
β βββ ArrayIndexOutOfBoundsException
β βββ IllegalArgumentException
βββ Checked Exceptions
βββ IOException
βββ SQLException
βββ ClassNotFoundException
Key Points:
- Checked: Must be caught or declared (compile-time check)
- Unchecked: Runtime exceptions, no mandatory handling
- finally: Always executes except System.exit() or JVM crash
- try-with-resources: Auto-closes resources implementing AutoCloseable
Interview Questions:
- Can we have try without catch? Yes, with finally or try-with-resources.
- What happens if exception thrown in finally? Overrides exception from try/catch.
π§΅ Part 4: Multithreading & Concurrencyβ
4.1 Thread Lifecycleβ
New β Runnable β Running β Blocked/Waiting/Timed Waiting β Terminated
4.2 Thread Creationβ
- Extend
Threadclass - Implement
Runnableinterface (preferred) - Implement
Callableinterface (returns value) - Use Lambda expressions with Runnable
4.3 Synchronizationβ
synchronized keyword:
- Method level: Locks entire object
- Block level: Locks specific object/code section
volatile:
- Ensures visibility of changes across threads
- No atomicity guarantee for compound operations
- Lighter than synchronization
Atomic Classes:
- AtomicInteger, AtomicLong, AtomicBoolean
- Use CAS (Compare-And-Swap) operations
4.4 Executor Frameworkβ
ExecutorService executor = Executors.newFixedThreadPool(10);
Future<Result> future = executor.submit(callable);
Types:
- FixedThreadPool
- CachedThreadPool
- ScheduledThreadPool
- SingleThreadExecutor
4.5 Advanced Concurrencyβ
wait(), notify(), notifyAll():
- Must be called within synchronized context
- wait() releases lock, sleep() doesn't
Deadlock Prevention:
- Lock ordering
- Lock timeout
- Deadlock detection
Common Problems:
- Producer-Consumer (using BlockingQueue)
- Reader-Writer problem
- Dining Philosophers
π¦ Part 5: Collections Frameworkβ
5.1 Collection Hierarchyβ
Collection
βββ List (ArrayList, LinkedList, Vector)
βββ Set (HashSet, LinkedHashSet, TreeSet)
βββ Queue (PriorityQueue, ArrayDeque)
Map (HashMap, LinkedHashMap, TreeMap, Hashtable, ConcurrentHashMap)
5.2 Key Comparisonsβ
| Feature | ArrayList | LinkedList |
|---|---|---|
| Access | O(1) | O(n) |
| Insert/Delete (middle) | O(n) | O(1) |
| Memory | Less | More |
| Feature | HashMap | Hashtable | ConcurrentHashMap |
|---|---|---|---|
| Thread-safe | No | Yes | Yes |
| Null key | 1 | No | No |
| Null value | Yes | No | No |
| Performance | Fast | Slow | Fast |
5.3 Important Conceptsβ
equals() and hashCode():
- If equals() returns true, hashCode() must be same
- Used in HashMap, HashSet
- Contract: Equal objects must have equal hash codes
fail-fast vs fail-safe:
- fail-fast: Throws ConcurrentModificationException (ArrayList, HashMap)
- fail-safe: Works on clone (CopyOnWriteArrayList, ConcurrentHashMap)
Comparable vs Comparator:
- Comparable: Natural ordering (compareTo method)
- Comparator: Custom ordering (compare method)
π Part 6: Java 8+ Featuresβ
6.1 Lambda Expressionsβ
// Before Java 8
Runnable r = new Runnable() {
public void run() { System.out.println("Hello"); }
};
// Java 8+
Runnable r = () -> System.out.println("Hello");
6.2 Functional Interfacesβ
- Predicate<T>: Test condition, returns boolean
- Function
<T, R>: Transform T to R - Consumer<T>: Accepts input, no return
- Supplier<T>: Provides value, no input
- BiFunction
<T, U, R>: Two inputs, one output
6.3 Stream APIβ
Common Operations:
// map: Transform elements
list.stream().map(String::toUpperCase)
// filter: Select elements
list.stream().filter(s -> s.length() > 5)
// flatMap: Flatten nested structures
list.stream().flatMap(Collection::stream)
// reduce: Aggregate to single value
list.stream().reduce(0, Integer::sum)
// collect: Mutable reduction
list.stream().collect(Collectors.toList())
Intermediate vs Terminal:
- Intermediate: Lazy, return Stream (map, filter, sorted)
- Terminal: Trigger execution (collect, forEach, reduce)
6.4 Optional Classβ
Optional<String> optional = Optional.ofNullable(value);
optional.ifPresent(System.out::println);
String result = optional.orElse("default");
String result = optional.orElseGet(() -> computeDefault());
6.5 Method Referencesβ
- Static:
ClassName::staticMethod - Instance:
instance::instanceMethod - Constructor:
ClassName::new
6.6 Default and Static Methods in Interfacesβ
Allow adding methods to interfaces without breaking implementations.
6.7 New Features (Java 9+)β
Records (Java 14):
record Person(String name, int age) {}
// Auto-generates constructor, getters, equals, hashCode, toString
Sealed Classes (Java 17):
sealed class Shape permits Circle, Rectangle {}
Text Blocks (Java 15):
String json = """
{
"name": "John"
}
""";
π¨ Part 7: Design Patternsβ
7.1 Creational Patternsβ
Singleton:
// Thread-safe lazy initialization
public class Singleton {
private static volatile Singleton instance;
private Singleton() {}
public static Singleton getInstance() {
if (instance == null) {
synchronized (Singleton.class) {
if (instance == null) {
instance = new Singleton();
}
}
}
return instance;
}
}
Factory Pattern: Creates objects without specifying exact class.
Builder Pattern: Constructs complex objects step by step.
Prototype Pattern: Creates objects by cloning existing ones.
7.2 Structural Patternsβ
Adapter: Converts interface to another expected by client Decorator: Adds functionality to objects dynamically Proxy: Provides surrogate/placeholder for another object Facade: Simplified interface to complex subsystem
7.3 Behavioral Patternsβ
Observer: One-to-many dependency (event listeners) Strategy: Family of algorithms, interchangeable Template Method: Skeleton algorithm, steps overridden Command: Encapsulates request as object
7.4 SOLID Principlesβ
- Single Responsibility: Class should have one reason to change
- Open/Closed: Open for extension, closed for modification
- Liskov Substitution: Subtypes must be substitutable for base types
- Interface Segregation: Many specific interfaces better than one general
- Dependency Inversion: Depend on abstractions, not concretions
π§ Part 8: Advanced Java Topicsβ
8.1 Serializationβ
// Serializable marker interface
class User implements Serializable {
private static final long serialVersionUID = 1L;
private String name;
private transient String password; // Not serialized
}
Externalization: Full control over serialization process
8.2 Reflection APIβ
Class<?> clazz = Class.forName("com.example.MyClass");
Method method = clazz.getDeclaredMethod("methodName");
method.setAccessible(true);
method.invoke(instance, args);
Use Cases: Frameworks, testing, annotations processing
8.3 Annotationsβ
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface CustomAnnotation {
String value() default "";
}
8.4 Genericsβ
public <T extends Comparable<T>> T findMax(List<T> list)
Type Erasure: Generic type information removed at runtime
Wildcards:
<?>: Unbounded wildcard<? extends T>: Upper bounded (read)<? super T>: Lower bounded (write)
8.5 Java Modules (JPMS - Java 9)β
module com.example.app {
requires java.sql;
exports com.example.api;
}
π» Part 9: Coding Problems (Practice)β
String Manipulationβ
- Reverse words in a sentence
- Check if string is palindrome
- Find first non-repeated character
- Remove duplicate characters
- Check if two strings are anagrams
- Count occurrences of each character
Arrays & Collectionsβ
- Find second largest element
- Remove duplicates from sorted/unsorted array
- Rotate array by k positions
- Find missing number in array
- Merge two sorted arrays
- Group anagrams together
HashMap/Set Problemsβ
- Two sum problem
- Find frequency of elements
- Longest substring without repeating characters
- Group elements by frequency
Multithreadingβ
- Print even/odd numbers using two threads
- Producer-Consumer problem
- Print sequence using three threads
- Implement thread-safe singleton
Java 8 Streamsβ
- Find frequency of each element
- Sort list of objects by multiple fields
- Partition list into even/odd numbers
- FlatMap nested lists
- Find top N elements
- Custom collectors
ποΈ Part 10: Low-Level Design (LLD)β
LLD Problem-Solving Approachβ
- Clarify Requirements: Ask questions about scope, constraints
- Identify Core Entities: Classes, relationships
- Define Relationships: Inheritance, composition, aggregation
- Apply Design Patterns: Where appropriate
- Consider SOLID Principles
- Handle Edge Cases
Common LLD Problemsβ
10.1 Parking Lot Systemβ
Requirements:
- Multiple floors, spots of different sizes
- Vehicle types: Car, Bike, Truck
- Entry/exit with ticket
- Payment calculation
Key Classes:
ParkingLot(Singleton)Floor,ParkingSpot(Abstract)Vehicle(Abstract)Ticket,PaymentParkingStrategy(Strategy pattern)
Design Considerations:
- Use Factory for vehicle creation
- Strategy pattern for spot allocation
- Observer pattern for spot availability
10.2 Elevator Systemβ
Requirements:
- Multiple elevators in building
- Up/down requests from floors
- Optimal elevator assignment
- Door operations
Key Classes:
ElevatorControllerElevator,Door,ButtonRequest(External/Internal)ElevatorSelectionStrategy
Algorithms:
- SCAN (elevator algorithm)
- LOOK algorithm
- Shortest Seek Time First
10.3 Library Management Systemβ
Requirements:
- Books, members, librarians
- Issue/return books
- Fine calculation
- Search functionality
Key Classes:
Library(Facade)Book,Member,LibrarianCatalog(search)Transaction,Fine
10.4 Hotel Booking Systemβ
Requirements:
- Search rooms by criteria
- Book/cancel reservations
- Room types, pricing
- Customer management
Key Classes:
Hotel,Room(types)Booking,CustomerSearchCriteria,RoomSearchServicePaymentService
10.5 ATM Machineβ
Requirements:
- Authentication
- Check balance, withdraw, deposit
- Cash dispenser management
- Transaction logging
Key Classes:
ATM,Card,AccountCashDispenserTransaction(types)AuthenticationService
Design Patterns:
- State pattern for ATM states
- Chain of Responsibility for cash dispensing
10.6 Online Shopping System (E-commerce)β
Key Classes:
User,Product,Cart,OrderInventory,Payment,ShippingSearchService,RecommendationEngine
10.7 Chess Gameβ
Requirements:
- Board setup, piece movements
- Turn management
- Check/checkmate detection
- Move validation
Key Classes:
Board,CellPiece(abstract), specific piecesPlayer,GameMoveValidator
10.8 Logging Frameworkβ
Requirements:
- Multiple log levels
- Different outputs (console, file)
- Thread-safe
- Configurable format
Key Classes:
Logger(Singleton)LogLevel(enum)Appender(interface) - Console, FileFormatter
Patterns: Singleton, Strategy, Template Method
10.9 Notification Serviceβ
Requirements:
- Multiple channels (Email, SMS, Push)
- Priority handling
- Retry mechanism
- Rate limiting
Key Classes:
NotificationServiceNotificationChannel(Strategy)NotificationQueueRetryHandler
10.10 Ride-Sharing System (Uber/Ola)β
Key Classes:
Driver,Rider,RideLocation,RoutePricingStrategyMatchingService
π Part 11: High-Level Design (HLD)β
HLD Approachβ
- Requirements Gathering: Functional & Non-functional
- Capacity Estimation: Users, requests, storage
- API Design: Endpoints, request/response
- Data Model: Database schema
- High-Level Components: Services, communication
- Scalability & Performance: Caching, load balancing
- Reliability: Replication, backup
- Security: Authentication, authorization
Common HLD Problemsβ
11.1 URL Shortener (TinyURL)β
Functional Requirements:
- Shorten long URL
- Redirect to original URL
- Custom aliases
- Analytics (optional)
Non-Functional:
- Low latency, high availability
- 100M URLs/day
Design:
- Encoding: Base62 (a-z, A-Z, 0-9)
- Database: NoSQL (key-value), sharded
- Caching: Redis for popular URLs
- API:
- POST /shorten β
{shortUrl} - GET /
{shortCode}β Redirect
- POST /shorten β
Components:
- Load Balancer
- Application Servers
- Database (sharded by hash)
- Cache Layer
- Analytics Service
11.2 Design Twitterβ
Functional:
- Tweet, follow/unfollow
- Timeline (home, user)
- Search tweets
Non-Functional:
- 500M users, 200M DAU
- Handle celebrity problem
Design:
- Data Model: User, Tweet, Follow tables
- Timeline Generation:
- Fan-out on write (pre-compute)
- Fan-out on read (compute on demand)
- Hybrid approach
- Components:
- Tweet Service
- Timeline Service
- Notification Service
- Search Service (Elasticsearch)
Technologies:
- Redis for timeline cache
- Kafka for event streaming
- Cassandra for tweets
- PostgreSQL for users
11.3 Design WhatsApp/Chat Systemβ
Functional:
- One-on-one messaging
- Group chat
- Sent/delivered/read receipts
- Media sharing
Non-Functional:
- Real-time delivery
- High availability
- End-to-end encryption
Design:
- Protocol: WebSocket for real-time
- Message Queue: Kafka/RabbitMQ
- Database:
- User data: SQL
- Messages: Cassandra (wide column)
- Storage: S3 for media
- Components:
- Connection Service (WebSocket)
- Message Service
- Group Service
- Notification Service
11.4 Design Netflix/YouTubeβ
Functional:
- Upload/stream videos
- Search, recommendations
- User profiles, subscriptions
Non-Functional:
- Low latency streaming
- High bandwidth
- Global availability
Design:
- CDN: Store videos closer to users
- Encoding: Multiple resolutions
- Adaptive Streaming: HLS/DASH
- Components:
- Upload Service (chunked upload)
- Encoding Service (queue-based)
- CDN (CloudFront, Akamai)
- Recommendation Engine (ML)
- Metadata DB (PostgreSQL)
11.5 Design Uber/Cab Bookingβ
Functional:
- Find nearby drivers
- Book ride, track location
- Payment, rating
Design:
- Location Service:
- QuadTree for spatial indexing
- Geohashing
- Matching Algorithm:
- Priority queue
- Proximity-based
- Components:
- Location Service
- Matching Service
- Ride Service
- Payment Service
- Notification Service
Databases:
- PostgreSQL (users, rides)
- Redis (driver locations)
- Cassandra (trip history)
11.6 Design Instagramβ
Functional:
- Upload photos, feed
- Follow users, like/comment
- Stories
Design:
- Feed Generation:
- Fan-out on write for regular users
- Fan-out on read for celebrities
- Storage: S3 for images
- Image Processing: Resize, thumbnails
- Database:
- PostgreSQL (users, relationships)
- Cassandra (posts, comments)
11.7 Design Dropbox/Google Driveβ
Functional:
- Upload/download files
- Sync across devices
- Sharing, versioning
Design:
- Chunking: Break files into chunks
- Deduplication: Hash-based
- Sync: Long polling/WebSocket
- Storage:
- Metadata DB (MySQL)
- Block storage (S3)
- Components:
- Upload/Download Service
- Sync Service
- Notification Service
11.8 Design Rate Limiterβ
Algorithms:
- Token Bucket
- Leaky Bucket
- Fixed Window Counter
- Sliding Window Log
- Sliding Window Counter
Implementation:
- Redis with TTL
- In-memory cache
- Distributed rate limiting
11.9 Design Distributed Cacheβ
Requirements:
- Get/Set operations
- Eviction policies (LRU, LFU)
- High availability
Design:
- Consistent Hashing: Distribute keys
- Replication: Master-slave
- Eviction: LRU using LinkedHashMap
- Sharding: Horizontal partitioning
11.10 Design Payment Systemβ
Functional:
- Process payments
- Handle multiple payment methods
- Refunds, transaction history
Non-Functional:
- ACID properties
- PCI DSS compliance
- Idempotency
Design:
- Database: PostgreSQL with ACID
- State Machine: Payment states
- Idempotency Keys: Prevent duplicate charges
- Components:
- Payment Gateway Integration
- Transaction Service
- Reconciliation Service
- Notification Service
π Part 12: System Design Conceptsβ
12.1 Scalability Patternsβ
- Vertical Scaling: Increase server capacity
- Horizontal Scaling: Add more servers
- Load Balancing: Distribute traffic
- Caching: Reduce database load
- CDN: Serve static content
- Database Sharding: Partition data
- Microservices: Decompose monolith
12.2 Database Conceptsβ
- SQL vs NoSQL: ACID vs BASE
- Replication: Master-slave, Master-master
- Sharding Strategies: Range, Hash, Directory
- Indexes: B-Tree, Hash, Full-text
- Normalization vs Denormalization
- CAP Theorem: Consistency, Availability, Partition Tolerance
12.3 Caching Strategiesβ
- Cache-Aside: Load data on cache miss
- Write-Through: Write to cache and DB simultaneously
- Write-Back: Write to cache, async to DB
- Refresh-Ahead: Preload data before expiry
12.4 Communication Patternsβ
- REST: Stateless, resource-based
- GraphQL: Flexible queries
- gRPC: High-performance RPC
- WebSocket: Bidirectional real-time
- Message Queues: Async communication
12.5 Distributed Systems Conceptsβ
- Consistency Models: Strong, Eventual, Causal
- Consensus Algorithms: Paxos, Raft
- Distributed Transactions: 2PC, Saga pattern
- Service Discovery: Eureka, Consul
- Circuit Breaker: Prevent cascading failures
π Part 13: Interview Tipsβ
Technical Interview Strategyβ
- Clarify Requirements: Ask questions before coding
- Think Aloud: Explain your thought process
- Start Simple: Basic solution, then optimize
- Consider Edge Cases: Null, empty, boundaries
- Analyze Complexity: Time and space
- Test Your Code: Walk through with examples
Common Mistakes to Avoidβ
- Jumping to code without understanding problem
- Not handling null/edge cases
- Poor variable naming
- Ignoring time/space complexity
- Not testing solution
Preparation Strategyβ
- Week 1-2: Core Java, OOP, Collections
- Week 3: Multithreading, Java 8+ features
- Week 4: Design patterns, SOLID principles
- Week 5-6: LLD problems (practice 2-3/day)
- Week 7-8: HLD problems, system design concepts
- Week 9-10: Coding practice (LeetCode/HackerRank)
- Week 11-12: Mock interviews, revision
Resourcesβ
- Books: Effective Java, Head First Design Patterns
- Coding: LeetCode, HackerRank, CodeSignal
- System Design: Grokking System Design, SystemDesignPrimer
- LLD: GitHub repositories, YouTube channels
β Final Checklistβ
Before Interviewβ
- Review core Java concepts
- Practice 5-10 coding problems
- Review 2-3 design patterns
- Prepare 1 LLD and 1 HLD problem
- Know your resume projects deeply
- Prepare questions to ask interviewer
During Interviewβ
- Listen carefully to requirements
- Ask clarifying questions
- Think before coding
- Communicate your approach
- Handle feedback gracefully
- Stay calm and confident
Key Takeawayβ
Master the fundamentals first, then move to advanced topics. Practice consistently, and don't just memorizeβunderstand the 'why' behind every concept.
Good luck with your interviews! π