0% found this document useful (0 votes)
5 views25 pages

Advanced Java Interview Master Handbook

The 'Advanced Java Interview Master Handbook' covers essential topics for mastering Java interviews, including algorithmic complexity, Java collections internals, JVM architecture, concurrency, and design patterns. It emphasizes understanding internals, trade-offs, and performance awareness, providing deep insights into concepts like HashMap behavior, garbage collection, and thread safety. The handbook prepares candidates for high-pressure interviews by simulating challenging questions and scenarios related to Java concurrency and system design.

Uploaded by

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

Advanced Java Interview Master Handbook

The 'Advanced Java Interview Master Handbook' covers essential topics for mastering Java interviews, including algorithmic complexity, Java collections internals, JVM architecture, concurrency, and design patterns. It emphasizes understanding internals, trade-offs, and performance awareness, providing deep insights into concepts like HashMap behavior, garbage collection, and thread safety. The handbook prepares candidates for high-pressure interviews by simulating challenging questions and scenarios related to Java concurrency and system design.

Uploaded by

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

Advanced Java Interview Master Handbook

SECTION 1: Algorithmic Thinking & Complexity


Mastery

1.1 Big-O Beyond Textbook Definitions

Tricky Interview Question

If HashMap put() is O(1), why can it become O(n)?


When exactly does that happen internally?

Deep Explanation

Time complexity is not just about average case. In Java


HashMap:

• Average: O(1)

• Worst case: O(n)

Why?
Because internally HashMap uses:

• Array of buckets

• Linked list (Java 7)

• Linked list + Red-Black Tree (Java 8+ when


bucket size > 8)

If:
• Many keys hash to same bucket

• Hash function is poor

• Or malicious collision attack

Then traversal becomes O(n).

Edge Case

Before Java 8: worst-case always O(n)


After Java 8: becomes O(log n) once treeified.

Follow-Up Trap

"If load factor is 0.75, why not 1.0?"


Answer: Trade-off between memory and rehash cost.
Higher load factor = fewer resizes but more collisions.

1.2 Amortized Analysis (Dynamic Array Growth)

Question

Why is [Link]() amortized O(1) but sometimes


O(n)?

Internal Working

When capacity full:

• New array created

• Elements copied
• Old array discarded

Growth formula (Java 8):


newCapacity = old + (old >> 1) // 1.5x growth

Why 1.5x?

• 2x wastes memory

• Smaller factor increases resize frequency

Real-world Insight

In high-frequency trading systems, resizing pause can


create latency spikes.
Solution: Pre-size ArrayList.

SECTION 2: Java Collections Deep Internals

2.1 [Link]() Internals

For primitives

Uses Dual-Pivot QuickSort.


Why?

• Cache-friendly

• In-place

• Faster for primitives


Worst case: O(n^2)
Average: O(n log n)

For objects

Uses TimSort.
Why?

• Stable

• Exploits natural runs

• Performs well on partially sorted data

Trick Question

Why different algorithms?


Answer:

• Objects need stability

• Primitives don't

• Object comparison expensive

2.2 HashMap Deep Dive

Internal Structure

Node<K,V>[] table

Each Node:

• hash
• key

• value

• next

Resize Trigger

threshold = capacity * loadFactor

When size > threshold → resize to 2x.

Treeification Condition

Bucket size > 8 AND capacity >= 64

Why >= 64?


To avoid tree overhead for small maps.

Common Mistake

Using mutable key.


If hashCode changes → retrieval fails.

2.3 Comparable vs Comparator

Comparable

Defines natural ordering.


Violating transitivity breaks sorting.

Comparator
External ordering logic.
Can chain comparators.

Trap

If compare() inconsistent with equals(), sorted


collections behave unpredictably.

SECTION 3: JVM & Memory Internals

3.1 JVM Architecture

Components:

• Heap

• Stack

• Metaspace

• Code Cache

Stack

Stores frames:

• Local variables

• Operand stack

Heap

• Eden
• Survivor spaces

• Old Gen

3.2 Garbage Collection

Algorithms

• Mark-Sweep

• Mark-Compact

• G1 (region-based)

G1 Insight

Splits heap into regions.


Targets pause-time goals.

Interview Trap

"Does GC guarantee memory cleanup?"


No. It reclaims unreachable objects only.

3.3 String Pool

String literals stored in pool.

"a" + "b" → compile-time constant


new String("ab") → heap object

Edge Case
Interning large dynamic strings can increase memory
pressure.

SECTION 4: Concurrency & Multithreading

4.1 synchronized vs ReentrantLock

synchronized:

• JVM-managed

• Automatic unlock

ReentrantLock:

• tryLock

• fairness option

Internal Detail

Both rely on monitor locks.


Modern JVM uses biased locking, lightweight locking.

4.2 ConcurrentHashMap Internals

Java 8 uses:

• CAS operations

• Synchronized blocks on bins


No full table lock.

SECTION 5: OOPS Edge Cases

5.1 Immutable Class Design

Rules:

• Final class

• Private final fields

• No setters

• Defensive copying

Edge Case

If field is mutable object, must deep copy in getter.

5.2 Polymorphism Trap

Method overloading resolved at compile time.


Overriding resolved at runtime.

SECTION 6: System Design (Java Backend)

6.1 Rate Limiter (Token Bucket)

Concept:
• Bucket with tokens

• Refill rate

• Each request consumes token

Java Implementation Sketch

Use AtomicLong for counters.

Trade-off:

• Memory vs accuracy

6.2 URL Shortener

Components:

• ID generator (Base62)

• Database

• Cache (Redis)

Edge Cases

• Collision handling

• Hot URLs

• Expiry strategy

6.3 Caching Strategies


• LRU

• LFU

• Write-through

• Write-back

Trade-off: Consistency vs latency.

SECTION 7: Agile & Engineering Excellence

Agile vs Scrum vs Kanban

Agile = philosophy
Scrum = framework
Kanban = flow-based model

SECTION 8: Design Patterns in Java

Singleton (Double-Checked Locking)

volatile required to prevent instruction reordering.

Factory Pattern

Encapsulates object creation.

Performance Comparison Table


Structure Avg Time Worst Time Notes

HashMap O(1) O(log n) Tree bins

ArrayList O(1)* O(n) Resize cost

LinkedList O(n) O(n) Poor cache locality

Final Interview Pressure Simulation

Interviewer: "Why is HashMap not thread-safe?"


Expected Depth:

• Race during resize

• Lost updates

• Structural corruption (infinite loop pre-Java 8)

Closing Note

This handbook emphasizes:

• Understanding internals

• Trade-offs

• Real-world constraints

• Performance awareness

Mastery comes from reasoning, not memorization.


SECTION 9: TRICKY JAVA CONCURRENCY MASTERY

This section is designed to simulate high-pressure


product-based company interviews where the
interviewer keeps digging until your mental model
collapses.

We will not memorize APIs.


We will understand memory, CPU, JVM, and lock
mechanics.

9.1 Java Memory Model (JMM) — The Foundation

Interview Question

"If two threads modify the same variable, why is


synchronization required even if the operation is
atomic?"

Deep Explanation

Java Memory Model defines:

• How threads interact through memory

• What guarantees visibility

• What guarantees ordering

Key Concepts:
• Heap is shared

• Each thread has its own working memory (CPU


cache + registers)

Problem:
Thread A updates variable x.
Thread B may still see stale value due to CPU caching.

This is NOT a JVM issue.


This is a hardware-level cache coherence issue.

Happens-Before Relationship

If A happens-before B, then:

• All writes by A are visible to B

Created by:

• synchronized block exit → subsequent synchronized


block entry

• volatile write → subsequent volatile read

• [Link]()

• [Link]()

Tricky Follow-Up

"Is volatile enough for i++?"


No.
Because:
1. Read

2. Increment

3. Write
Three steps. Not atomic.
Volatile guarantees visibility, not atomicity.

9.2 Race Conditions & Visibility Bugs

Example

int count = 0;

Thread A: count++;
Thread B: count++;

Possible result: 1 instead of 2.

Why?
Because both threads read 0.
Then both write 1.

Memory-Level Breakdown

CPU Instruction Approximation:


LOAD count
INC register
STORE count

Interleaving creates lost update.


9.3 synchronized — What Actually Happens Internally

Interview Trap

"Is synchronized a keyword or a lock?"

It is a monitor-based locking mechanism.


Every object has:

• Monitor

• Mark Word (in object header)

Object Header Contains:

• Hash code

• GC age

• Lock state bits

Lock States:

1. Biased Lock

2. Lightweight Lock

3. Heavyweight Lock

Lock Upgrade Path

Biased → Lightweight → Heavyweight


Never downgraded.
Why?
Downgrading would require global coordination.

Performance Insight

Modern JVM optimized synchronized heavily.


In low contention cases, it is extremely fast.

9.4 ReentrantLock vs synchronized

Feature synchronized ReentrantLock

Interruptible No Yes

Fairness No Optional

tryLock No Yes

Multiple conditions No Yes

Internal Difference

ReentrantLock built on AQS


(AbstractQueuedSynchronizer).

AQS uses:

• CLH Queue

• CAS operations

• State variable
This is not monitor-based locking.
It is framework-level lock.

Tricky Question

"Which is faster?"
Depends on contention.
Low contention → synchronized competitive.
High contention → ReentrantLock better due to
flexibility.

9.5 Volatile — When and When NOT

Volatile guarantees:

• Visibility

• No instruction reordering across volatile boundary

Volatile does NOT guarantee:

• Atomicity

• Mutual exclusion

Correct Use Case

Status flags:
private volatile boolean running;

Wrong Use Case


Counters without atomic operations.

9.6 Atomic Classes & CAS

Compare-And-Swap (CAS)

Hardware-level atomic instruction.

Pseudo:
if (current == expected)
update
else
retry

AtomicInteger uses CAS loop.

Problem: ABA

Value changes A → B → A.
CAS thinks nothing changed.

Solution:
AtomicStampedReference

Interview Follow-Up

"Is CAS always better than locking?"


No.
Under heavy contention → spinning wastes CPU.
Locks may perform better.
9.7 ConcurrentHashMap Deep Internals (Java 8+)

Pre-Java 8:

• Segment-based locking

Java 8:

• No segments

• Uses CAS + synchronized on bins

Insertion Logic:

1. Try CAS to insert

2. If collision → synchronize bin

3. If bin > 8 → treeify

No global lock.
High concurrency achieved.

Tricky Edge Case

size() is approximate under concurrency.

9.8 Thread Pools Deep Dive

[Link]()

Uses:
• ThreadPoolExecutor

• LinkedBlockingQueue

Core Parameters:

• corePoolSize

• maximumPoolSize

• keepAliveTime

• workQueue

• rejectionHandler

Tricky Interview Question

"Why can fixed thread pool cause OOM?"


Because LinkedBlockingQueue is unbounded by default.
Tasks accumulate infinitely.

Solution:
Use bounded queue.

9.9 Deadlock Analysis

Conditions:

1. Mutual exclusion

2. Hold and wait

3. No preemption
4. Circular wait

Code Smell

Nested synchronized blocks in inconsistent order.

Production Strategy

• Lock ordering

• Timeout locks

• Monitoring via jstack

9.10 Livelock vs Deadlock vs Starvation

Deadlock: Threads blocked forever.


Livelock: Threads active but no progress.
Starvation: Thread never gets CPU time.

Interview often asks difference.

9.11 False Sharing — Advanced Performance Trap

Two threads update different variables.


Still slow.

Why?
Because variables share same cache line.
CPU invalidates entire line.
Solution:

• Padding

• @Contended annotation

This is advanced and impresses interviewers.

9.12 ForkJoinPool & Work Stealing

Used in parallel streams.

Mechanism:

• Each worker has deque

• Idle threads steal from others

Efficient for recursive divide-and-conquer.

Trap

Blocking inside ForkJoinPool reduces performance.

9.13 CompletableFuture Deep Dive

Non-blocking async programming.

Execution:

• Default: [Link]()

Chaining creates dependency graph.


Common Mistake:
Using join() unnecessarily → blocks thread.

9.14 Designing Thread-Safe Classes

Approaches:

1. Immutability

2. Confinement

3. Synchronization

4. Lock-free structures

Golden Rule:
Minimize shared mutable state.

9.15 Production-Level Concurrency Design Question

"Design a rate limiter that supports 1M requests/sec."

Expected Thinking:

• Avoid global lock

• Use atomic counters

• Use time bucket strategy

• Consider distributed setup


Discuss trade-offs:

• Accuracy vs performance

• Memory vs precision

9.16 Common Interview Mistakes

• Confusing visibility with atomicity

• Thinking volatile replaces synchronized

• Ignoring memory model

• Using Executors factory blindly

• Not considering false

You might also like