Core Java & Data Structures
1. Explain the internal working of ConcurrentHashMap. How does it achieve thread safety, and what are its performance trade-offs?
Internal Working:
- Prior to Java 8, it used segment-based locking, where the map was divided into segments, each with its own lock.
- In Java 8+, it uses a bucket-level lock-free approach with CAS (Compare-And-Swap) for updates.
- The table is backed by Node<K, V>[] with each entry being a linked list or a tree (if size > 8).
- Uses synchronized blocks for initialization and volatile for visibility.
Thread Safety Mechanism:
- Uses CAS for atomic updates and synchronized blocks where necessary.
- Prevents full-table locks by optimistic concurrency control.
- Buckets reduce lock contention compared to
synchronizedMap
.
Trade-offs:
- Better scalability than
Hashtable
,synchronizedMap
. - Memory overhead due to linked-list/tree structures.
- More CPU cycles in highly concurrent scenarios.
2. WeakHashMap vs HashMap. When would you use each?
Feature | HashMap | WeakHashMap |
---|---|---|
Key Removal | Never removes keys unless explicitly deleted | Removes keys when no strong reference exists |
Use Case | General-purpose storage | Cache or metadata where automatic cleanup is needed |
GC Impact | Keys are retained indefinitely | Entries are removed when GC runs |
Use Cases:
- Use
WeakHashMap
for temporary metadata storage where memory cleanup is required. - Use
HashMap
for general-purpose data storage where explicit key management is needed.
3. Efficiently searching for duplicate transactions in a large dataset (millions of records)
- Approach 1: Hashing → Store transactions in a
HashSet
to detect duplicates in O(1) time. - Approach 2: Bloom Filters → For space-efficient duplicate detection.
- Approach 3: Distributed Approach (Redis, Kafka, SQL with Indexing)
4. Java Memory Model Impact on Multi-threaded Applications
- Defines happens-before relationship to ensure visibility.
- Use of
volatile
,synchronized
,Lock
,Atomic
variables ensures memory consistency. - Helps prevent data races and visibility issues.
5. In-memory Key-Value Store with TTL
- Use ConcurrentHashMap + ScheduledExecutorService to remove expired keys.
- Alternative: Redis with EXPIRE commands.
Multithreading & Concurrency
1. Designing a Thread Pool from Scratch
- Use
BlockingQueue<Runnable>
to store tasks. - Worker threads poll from the queue and execute tasks.
ThreadFactory
for custom thread creation.shutdown()
for graceful termination.
2. False Sharing in Multi-Core Processors
- Occurs when multiple threads modify variables in the same cache line.
- Solution: Use padding (
@Contended
) or separate cache lines.
3. Synchronized Methods vs ReentrantLock
synchronized
→ Simpler, automatic unlocking.ReentrantLock
→ More control, fairness, tryLock, better performance in high contention.
4. Implementing a Multi-threaded Rate Limiter
- Use Token Bucket or Leaky Bucket algorithm.
- Implement using Redis + Lua scripting.
5. Handling Thread Starvation
- Use priority queues, fair locks, thread pool tuning.
Spring Boot & Microservices
1. Spring Boot Auto-Configuration
- Uses
@ConditionalOnClass
,@ConditionalOnProperty
. - Reads
spring.factories
to decide which beans to load.
2. Circuit Breaker Pattern
- Implemented using Resilience4j or Hystrix.
- Prevents cascading failures by cutting off overloaded services.
3. Blue-Green Deployments
- Deploy new versions on separate infrastructure.
- Use Kubernetes, Load Balancers for zero-downtime switch.
4. Eventual Consistency in Microservices
- Use Sagas, Outbox Pattern, Kafka for reliable data sync.
5. Rate-Limited API in Spring Boot
- Use Bucket4j or Redis + Lua to handle 10K requests/sec.
APIs & RESTful Services
1. Designing an API Gateway
- Use Spring Cloud Gateway, Kong, or NGINX.
- Handle rate limiting, JWT security, load balancing.
2. Pagination for Large Datasets
- Keyset pagination is preferred over offset.
- Use caching for frequently accessed pages.
3. API Timeouts & Retries
- Exponential backoff strategy with
Resilience4j
.
4. Implementing WebSockets in Fintech Apps
- Use Spring WebFlux + STOMP + RabbitMQ/Kafka.
5. Enforcing Idempotency in Payment APIs
- Use Idempotency keys stored in Redis/DB.
System Design Basics
1. High-Throughput Order Matching System
- Use priority queues, Kafka for event streaming.
- Match orders in O(log N) time using binary heaps.
2. Multi-Region Database Integrity
- Leader-follower replication, CRDTs, or distributed transactions.
3. Leader Election in Microservices
- Use Zookeeper, Consul, or Raft Algorithm.
4. CQRS vs CRUD
Feature | CQRS | CRUD |
---|---|---|
Read Scalability | High | Limited |
Write Complexity | High | Simple |
5. Handling Backpressure in Kafka
- Use Consumer Lag Monitoring, Batching, Adaptive Throttling.
Summary & Tips
- Focus on Concurrency, API Optimization, and Microservices Resilience.
- Hands-on experience with Kafka, Redis, Resilience4j, Rate Limiting is crucial.
- Practice system design and coding problems on LeetCode, Design Gurus.
This document should serve as a comprehensive interview guide for PayPal's backend technical round!
Comments
Post a Comment