Java Backend Developer - Interview Q&A
Q: Diff between HashMap and ConcurrentHashMap
A: - HashMap: Not thread-safe, allows null keys/values, fail-fast iterator.
- ConcurrentHashMap: Thread-safe, no nulls allowed, weakly consistent iterators, uses segmented locking.
Q: Find 2 elements whose sum = target (Java)
A: Use HashMap to store complements while iterating.
Time: O(n), Space: O(n).
Q: Encapsulation vs Abstraction vs Polymorphism vs Inheritance
A: - Encapsulation: Data hiding via getters/setters.
- Abstraction: Hiding implementation via abstract class/interface.
- Polymorphism: One entity, many forms (overloading/overriding).
- Inheritance: Acquire properties/behavior of another class.
Q: Abstract Class vs Interface
A: - Abstract class: Can have state, constructors, abstract & concrete methods. Single inheritance.
- Interface: Only abstract methods (plus default/static since Java 8). Multiple inheritance supported.
Q: How to create Lambda function
A: - Syntax: (params) -> expression or (params) -> { body }.
- Requires functional interface.
Example: Runnable r = () -> [Link]("Hi");
Q: Fail-Fast vs Fail-Safe Iterators
A: - Fail-Fast: Throws ConcurrentModificationException, works on actual collection.
- Fail-Safe: Works on clone, no exception, weakly consistent.
Q: Multithreading Real-Time Example
A: Bank ATM withdrawal simulation with synchronized methods to prevent race conditions.
Q: Modules used in Multithreading Spring apps
A: - @Async with ThreadPoolTaskExecutor
- @Scheduled
- Spring Batch for parallel steps
- CompletableFuture
- Spring Integration/Kafka for async messaging.
Q: Kafka Partition Assignment
A: - With key: hash(key) % partitions.
- No key: round-robin.
- Custom partitioner possible by implementing Partitioner class.
Q: Queue to Stack Conversion
A: - Poll from Queue and push into Stack (LinkedList-based or [Link]).
Q: Stack using Queue
A: - Use two queues: push into q2, move all q1 into q2, swap. Pop from q1.
Q: Queue using Stack
A: - Use two stacks: enqueue -> push to s1. dequeue -> move s1 to s2 if empty, then pop from s2.
Q: Bean Validation Annotations in Spring Boot DTO
A: - @NotNull, @NotBlank, @Size, @Email, @Pattern, @Min, @Max, @Past, @Future, @Valid (for nested).
Q: Two-Phase Commit (2PC)
A: - Phase 1: Prepare (participants vote YES/NO).
- Phase 2: Commit/Rollback based on votes.
- Ensures atomicity in distributed systems.
Q: Circuit Breaker Pattern
A: - Prevents repeated calls to failing service.
- States: Closed, Open, Half-Open.
- Implemented using Resilience4j/Hystrix.
Q: Spring Boot Exception Handling
A: - Use @ControllerAdvice + @ExceptionHandler.
- Custom ErrorResponse DTO for structured errors.
Q: SQL Query Optimization Strategies
A: - Use indexes, avoid SELECT *, filter early, prefer JOINs, avoid functions on indexed cols, paginate large
results, analyze execution plan.
Q: Group Employees by Department using Streams
A: - [Link](EmployeeDTO::getDepartment).
Q: How Streams API Works Internally
A: - Stream = pipeline (source -> intermediate -> terminal).
- Lazy evaluation.
- Uses Spliterator.
- Parallel streams use ForkJoinPool.
Q: Spring Bean Scopes
A: - Singleton, Prototype, Request, Session, Application, WebSocket.
- Real-time: Singleton for services, Request for per-API, Session for user state.
Q: Spring Boot Logging Override
A: - Configure logging in [Link] or [Link].
- Define log levels, appenders, patterns.
- Use interceptors/filters to log API calls.
Q: Knowing Method Call without Adding Logging Code
A: - Use Spring AOP with @Around advice to intercept method calls.
- Use @Async to log in separate thread.
Q: Single-Time Async Button (Frontend use case)
A: - Prevents duplicate requests.
- Disables button after click until async call finishes.
- Useful for payment or form submission.
Q: Design Patterns Overview
A: - Creational: Singleton, Factory, Builder.
- Structural: Adapter, Decorator, Proxy.
- Behavioral: Observer, Strategy, Command.
Q: Factory Pattern Use Case
A: - Example: NotificationFactory creates Email/SMS/Push notification objects.
- Purpose: Centralized creation, decouples client from concrete classes.