These are the notes for Low-Level Design (LLD) episode 56. In episode 55 our library design ran, with a demo, inside 90 minutes. Now the interviewer asks follow-up questions. We answer three classic ones in code, on the same library: adding a new feature without breaking code, making it thread-safe, and scaling an in-memory design with a persistence swap via repositories. Then the talking: explaining trade-offs, justifying pattern choices, defending against over-engineering, and design review vocabulary.
How the code was run: every program with javac Main.java && java -cp . Main on OpenJDK 21. All outputs are real. The two thread programs print their first lines in whichever order the threads finish, so your run may show them swapped.
1. What follow-up questions test
A follow-up question is a new requirement or a "what if" asked after your code runs. It tests whether the design bends without breaking, not whether you can type more.
| Family | Example | What it checks |
|---|---|---|
| Add a feature | "Add late fines" | Is there an extension point? |
| Concurrency | "Two counters at once?" | Is shared state safe? |
| Scale / storage | "Fifty branches? A database?" | Is storage swappable? |
| Justify | "Why this pattern?" | Can you explain the trade-off? |
A good design makes each answer small: a new class, one keyword, one swapped object.
2. The real-life idea: the railway booking office
| At the booking office | In the design |
|---|---|
| A new senior-citizen concession is added to the fare rules, not by rebuilding the counter | Extension point: a new rule is a new class |
| Two clerks both see one berth free, and both sell it | Race condition; fix with thread safety (check and take in one step) |
| Every station asks one central reservation chart | Single source of truth: one shared store |
| A tailor asks "why?" before adding pockets: they cost cloth | Trade-off: every choice has a price |
3. Follow-up 1: add late fines
Students pay Rs 2 a day, staff pay nothing; then the interviewer adds "visitors pay Rs 10 a day, capped at Rs 100".
The quick fix
class Library {
long fineFor(String memberType, int daysLate) { // the quick fix: one more if per type
if (memberType.equals("STUDENT")) return 2L * daysLate;
else if (memberType.equals("STAFF")) return 0;
return 0; // anyone else: no fine
}
}
public class Main {
public static void main(String[] args) {
Library lib = new Library();
System.out.println("Student, 30 days late: Rs " + lib.fineFor("STUDENT", 30));
System.out.println("Staff, 30 days late: Rs " + lib.fineFor("STAFF", 30));
// the interviewer: "Visitors pay Rs 10 a day, capped at Rs 100."
System.out.println("Visitor, 30 days late: Rs " + lib.fineFor("VISITOR", 30));
}
}
Output (real):
Student, 30 days late: Rs 60
Staff, 30 days late: Rs 0
Visitor, 30 days late: Rs 0
The visitor was fined Rs 0, silently. Every new member type means editing Library, the core class, and every edit risks the rules that already worked.
Adding a new feature without breaking code
- Ask what varies: the fine rule, per member.
- Give the variation an extension point (also called a seam): a place built for new behaviour to plug in. Here,
interface FinePolicy { long fine(int daysLate); }. - Each rule becomes a class: Open-Closed Principle (episode 13), open for extension, closed for modification.
- In pattern names, each policy is a Strategy (episode 34) and the cap is a Decorator (episode 28): it wraps another policy and limits its answer.
import java.util.*;
interface FinePolicy { long fine(int daysLate); } // the extension point
record PerDay(long rupees) implements FinePolicy { public long fine(int d) { return rupees * d; } }
record NoFine() implements FinePolicy { public long fine(int d) { return 0; } }
record Capped(FinePolicy inner, long max) implements FinePolicy { // new rule = new class
public long fine(int d) { return Math.min(max, inner.fine(d)); }
}
record Member(String name, FinePolicy policy) {}
class Library { // untouched by new rules
long giveBack(Member m, String isbn, int daysLate) {
return m.policy().fine(Math.max(0, daysLate));
}
}
public class Main {
public static void main(String[] args) {
Library lib = new Library();
List<Member> members = List.of(new Member("Asha (student)", new PerDay(2)),
new Member("Ravi (staff)", new NoFine()),
new Member("Priya (visitor)", new Capped(new PerDay(10), 100)));
for (Member m : members)
System.out.println(m.name() + ", 30 days late: Rs " + lib.giveBack(m, "B1", 30));
}
}
Output (real):
Asha (student), 30 days late: Rs 60
Ravi (staff), 30 days late: Rs 0
Priya (visitor), 30 days late: Rs 100
Visitors arrived as one new class (Capped); Library and the tests for the old rules never changed.
4. Follow-up 2: two counters at once
A thread (episode 46) is an independent path of execution inside one program. Two counters = two threads calling borrow at the same moment.
class Shelf {
private int copies = 1; // the last copy of Head First Java
boolean borrow(String who) {
if (copies > 0) { // check ...
pause(100); // (say, a slow call to save the loan)
copies--; // ... then act
return true;
}
return false;
}
int copies() { return copies; }
static void pause(int ms) { try { Thread.sleep(ms); } catch (InterruptedException e) { } }
}
public class Main {
public static void main(String[] args) throws InterruptedException {
Shelf shelf = new Shelf();
Thread counter1 = new Thread(() -> System.out.println("counter 1, Ravi: " + shelf.borrow("Ravi")));
Thread counter2 = new Thread(() -> System.out.println("counter 2, Neha: " + shelf.borrow("Neha")));
counter1.start(); counter2.start();
counter1.join(); counter2.join();
System.out.println("copies left: " + shelf.copies());
}
}
Output (real; the first two lines can appear in either order):
counter 1, Ravi: true
counter 2, Neha: true
copies left: -1
Both threads checked, both saw one copy, both took it. A race condition is a bug where the result depends on the timing of threads. Check-then-act (check a condition, then act on it, as two separate steps) is the classic way to get one. The pause(100) stands in for real work between the check and the act (saving the loan), and makes the race happen on every run.
Making it thread-safe
A class is thread-safe when it stays correct even if many threads call it at the same time.
- Find the shared, changing state: the copy count (and loans).
- Make the check and the act one atomic step: atomic means it happens all at once, with no other thread able to slip in between.
| Option | How | Trade-off |
|---|---|---|
synchronized borrow |
One lock for the whole method | Simplest, obviously correct; every borrow waits in one line |
| One lock per book | A lock object per ISBN | Busy books don't block quiet ones; more code, lock ordering if you hold two |
ConcurrentHashMap.compute |
Atomic update of one map entry | Fast and fine-grained; logic must fit in one function |
synchronized boolean borrow(String who) { // one counter at a time
Output (real; which counter wins varies between runs, but never two winners):
counter 2, Neha: false
counter 1, Ravi: true
copies left: 0
Going further: 100 threads, 5 copies, no global lock
ConcurrentHashMap.computeIfPresent runs its function atomically for one key, so each book has its own "lock":
import java.util.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;
class Shelves { // per-book atomic update, no global lock
private final ConcurrentHashMap<String, Integer> copies = new ConcurrentHashMap<>();
void add(String isbn, int n) { copies.merge(isbn, n, Integer::sum); }
boolean borrow(String isbn) {
boolean[] took = {false};
copies.computeIfPresent(isbn, (k, left) -> { // runs atomically for this one key
if (left == 0) return left;
took[0] = true;
return left - 1;
});
return took[0];
}
int copies(String isbn) { return copies.getOrDefault(isbn, 0); }
}
public class Main {
public static void main(String[] args) throws Exception {
Shelves shelves = new Shelves();
shelves.add("B2", 5);
ExecutorService pool = Executors.newFixedThreadPool(8);
AtomicInteger lent = new AtomicInteger();
List<Future<?>> jobs = new ArrayList<>();
for (int i = 0; i < 100; i++) // 100 students race for 5 copies
jobs.add(pool.submit(() -> { if (shelves.borrow("B2")) lent.incrementAndGet(); }));
for (Future<?> f : jobs) f.get();
pool.shutdown();
System.out.println("lent: " + lent.get() + ", copies left: " + shelves.copies("B2"));
}
}
Output (real, same on every run):
lent: 5, copies left: 0
5. Follow-up 3: fifty branches, many servers
A load balancer is a component that sends each incoming request to one of several servers.
import java.util.*;
interface CopiesRepository { int copies(String isbn); void save(String isbn, int copies); }
class InMemoryCopiesRepository implements CopiesRepository { // a HashMap in THIS server's memory
private final Map<String, Integer> rows = new HashMap<>(Map.of("B2", 1));
public int copies(String isbn) { return rows.getOrDefault(isbn, 0); }
public void save(String isbn, int copies) { rows.put(isbn, copies); }
}
class Library {
private final CopiesRepository repo;
Library(CopiesRepository repo) { this.repo = repo; }
synchronized boolean borrow(String isbn) {
int left = repo.copies(isbn); if (left == 0) return false;
repo.save(isbn, left - 1); return true;
}
}
public class Main {
public static void main(String[] args) {
Library server1 = new Library(new InMemoryCopiesRepository()); // two servers, one library
Library server2 = new Library(new InMemoryCopiesRepository());
System.out.println("server 1 lends B2: " + server1.borrow("B2"));
System.out.println("server 2 lends B2: " + server2.borrow("B2"));
Library restarted = new Library(new InMemoryCopiesRepository()); // server 1 restarts
System.out.println("after a restart, server 1 lends B2: " + restarted.borrow("B2"));
}
}
Output (real):
server 1 lends B2: true
server 2 lends B2: true
after a restart, server 1 lends B2: true
Three loans of a one-copy book. synchronized only protects threads inside one program (one JVM); each server's HashMap holds its own copy of the truth, and a restart wipes memory.
Why an in-memory design stops scaling
| Problem | Why |
|---|---|
| Different truths | Each server's memory is separate |
| Data loss | Memory is cleared on restart or crash |
| Size limit | One machine's RAM (working memory) limits the data |
The fix is one shared, durable store (durable = survives restarts), usually a database: a single source of truth, the one place whose answer counts.
The lock must move into the store too, because servers can't share a Java lock. Two common ways:
- An atomic conditional update, for example in SQL:
UPDATE books SET copies = copies - 1 WHERE isbn = ? AND copies > 0. If it changed one row, the copy was taken; if zero rows, it was already gone. - Optimistic locking (episode 50): each row has a
versionnumber; an update succeeds only if the version is still the one you read.
6. Persistence swap via the repository
Because Library only knows the CopiesRepository interface (episode 55's decision), we swap the class behind it. Here a folder of files stands in for the database so it runs in the compiler; Library is identical to the in-memory version.
import java.io.*; import java.nio.file.*;
interface CopiesRepository { int copies(String isbn); void save(String isbn, int copies); }
class FileCopiesRepository implements CopiesRepository { // same interface, data on disk
private final Path dir; FileCopiesRepository(Path dir) { this.dir = dir; }
public int copies(String isbn) {
try { return Integer.parseInt(Files.readString(dir.resolve(isbn)).trim()); }
catch (NoSuchFileException e) { return 0; } catch (IOException e) { throw new UncheckedIOException(e); } }
public void save(String isbn, int copies) {
try { Files.writeString(dir.resolve(isbn), String.valueOf(copies)); }
catch (IOException e) { throw new UncheckedIOException(e); } }
}
class Library { // not one line changed
private final CopiesRepository repo;
Library(CopiesRepository repo) { this.repo = repo; }
synchronized boolean borrow(String isbn) {
int left = repo.copies(isbn); if (left == 0) return false;
repo.save(isbn, left - 1); return true;
}
}
public class Main {
public static void main(String[] args) throws IOException {
Path shared = Files.createTempDirectory("library"); // one store, both servers
new FileCopiesRepository(shared).save("B2", 1);
Library server1 = new Library(new FileCopiesRepository(shared));
Library server2 = new Library(new FileCopiesRepository(shared));
System.out.println("server 1: " + server1.borrow("B2") + ", server 2: " + server2.borrow("B2"));
System.out.println("after a restart, B2 copies: " + new FileCopiesRepository(shared).copies("B2"));
}
}
Output (real):
server 1: true, server 2: false
after a restart, B2 copies: 0
Server 2 was refused because it read the shared count, and the count survived a "restart" (a brand-new repository object). Note that the two Library objects here run one after another in one program; two real processes racing on the same files would still need the store to make check-and-take atomic, as above.
7. Explaining trade-offs
A trade-off is a choice where you gain one thing by giving up another. Template:
I chose X, because Y. The cost is Z. I'd switch to W if V.
Example: "I chose a synchronized borrow because it's simple and obviously correct. The cost is one queue for all books. I'd move to per-book locks if counters start waiting too long."
More examples:
| Decision | Because | Cost | Switch when |
|---|---|---|---|
| In-memory repository | Fast to build, no setup | Lost on restart, one machine | Persistence or more servers are required |
FinePolicy interface |
Fines vary by member type | One more type to read | (Keep: the variation is real) |
| Records for entities | Immutable, short | A change creates a new object | Entities need identity-based updates |
| Unchecked domain exceptions | Clean signatures | Callers aren't forced to handle them | A failure must always be handled by the caller |
8. Justifying pattern choices and defending against over-engineering
Over-engineering is adding structure the problem doesn't need yet:
interface FinePolicyFactoryProvider {
FinePolicyFactory provide(Context c);
}
abstract class AbstractFinePolicyFactory
implements FinePolicyFactory { ... }
class FinePolicyFactoryProviderImpl
implements FinePolicyFactoryProvider { ... }
class FineEventBus { ... }
class FinePolicyBuilder { ... }
// 6 types, 0 new behaviour
Justifying a pattern means naming the problem it solves:
interface FinePolicy {
long fine(int daysLate);
}
record PerDay(long rupees) implements FinePolicy { ... }
record NoFine() implements FinePolicy { ... }
record Capped(FinePolicy inner, long max)
implements FinePolicy { ... }
// 1 interface: one reason (fines vary by member)
YAGNI (episode 17): "you aren't gonna need it", so don't build it until a real requirement asks.
When asked "why not add a factory here?":
- Name the problem the pattern would solve.
- If there is none today, say when you'd add it ("when members are created from three kinds of user input").
- Point to the seam that makes adding it cheap later ("members are created in one place, so it's a ten-minute change").
"Not yet, and here's why it's cheap to add later" is a stronger answer than adding it.
9. Design review vocabulary
A design review is a meeting where engineers examine a design before or after it's built.
| Word | Meaning | Library example |
|---|---|---|
| Coupling | How much one class depends on another (want low) | Library knows only CopiesRepository, not files or maps |
| Cohesion | How well a class's parts belong together (want high) | FinePolicy classes only compute fines |
| Invariant | A rule that must always be true | copies ≥ 0 |
| Extension point / seam | Where new behaviour plugs in | FinePolicy, CopiesRepository |
| Atomic | Happens all at once, nothing in between | check + take one copy |
| Race condition | Result depends on thread timing | two counters, copies = -1 |
| Single source of truth | The one store whose answer counts | the shared database |
| Trade-off | Gain one thing, pay with another | synchronized: simple, one queue |
| YAGNI | Don't build it until it's needed | no FinePolicyFactoryProvider |
Common mistakes
| Mistake | What you see (real) | Why | Fix |
|---|---|---|---|
| if-else per new type | Visitor, 30 days late: Rs 0 |
No extension point | An interface per varying rule |
| Check, then act, unlocked | copies left: -1 |
Race condition | One atomic step |
| "synchronized fixes everything" | 3 loans of a 1-copy book on 2 servers | Locks are per program | Atomic update in the shared store |
Library creates its own HashMap |
Can't swap storage | Depends on a concrete class | Inject a repository |
| Patterns with no problem | 6 new types, 0 new behaviour | Over-engineering | Name the problem, or YAGNI |
Mini project: a faculty fine rule and a grace period
Homework solution: faculty get 7 free days, then Rs 1 a day, added as one new class (GraceDays, another decorator). Nothing else changes.
import java.util.*;
interface FinePolicy { long fine(int daysLate); }
record PerDay(long rupees) implements FinePolicy { public long fine(int d) { return rupees * d; } }
record NoFine() implements FinePolicy { public long fine(int d) { return 0; } }
record Capped(FinePolicy inner, long max) implements FinePolicy { public long fine(int d) { return Math.min(max, inner.fine(d)); } }
record GraceDays(int days, FinePolicy inner) implements FinePolicy { // homework: faculty get 7 free days
public long fine(int d) { return inner.fine(Math.max(0, d - days)); }
}
public class Main {
public static void main(String[] args) {
FinePolicy faculty = new GraceDays(7, new PerDay(1));
for (int d : new int[] {3, 7, 10, 30})
System.out.println("faculty, " + d + " days late: Rs " + faculty.fine(d));
FinePolicy visitor = new Capped(new PerDay(10), 100);
System.out.println("visitor, 5 days late: Rs " + visitor.fine(5));
}
}
Output (real):
faculty, 3 days late: Rs 0
faculty, 7 days late: Rs 0
faculty, 10 days late: Rs 3
faculty, 30 days late: Rs 23
visitor, 5 days late: Rs 50
Practice
1. The interviewer says "students in their final year pay half". Where does that go?
Answer
A new policy, e.g. record Half(FinePolicy inner) implements FinePolicy { public long fine(int d) { return inner.fine(d) / 2; } }, assigned to final-year members. Library doesn't change.
2. Why does synchronized not help when the app runs on two servers?
Answer
A synchronized lock lives inside one JVM. Two servers are two JVMs with separate locks and separate memory; the shared store must make the update atomic.
3. Write the trade-off sentence for choosing an in-memory repository.
Answer
"I chose an in-memory repository because it's fast to build and enough for one server. The cost is that data is lost on restart and can't be shared. I'd switch to a database-backed repository when we need persistence or more than one server; Library won't change because it depends on the interface."
4. Name the shared, changing state in the library and how you'd protect each piece.
Answer
Copy counts per book and loans per member. Borrow must update both together, so either one lock around both (simple), or per-member and per-book locks taken in a fixed order (finer, but watch for deadlock, episode 50), or a single atomic database transaction.
5. An interviewer asks "would you add an event bus for fines?" How do you answer?
Answer
"Not yet: nothing listens for fines today. If we add SMS reminders or accounting that must react to fines, I'd publish a FineCharged event (Observer). giveBack is the one place fines are created, so that's a small change."
Quick quiz
borrow() is synchronized; the app runs on 3 servers behind a load balancer sharing one database. Can two servers lend the last copy? (a) No: synchronized prevents it (b) Yes, unless the database does the check and the update in one atomic step (c) No: the load balancer prevents it (d) Only if the servers restart
Answer
(b). synchronized only orders threads inside one JVM. Across servers the shared store must make check-and-take atomic, e.g. UPDATE ... WHERE copies > 0, or optimistic locking with a version column.
Interview questions
- How do you add a feature without breaking existing code? Find what varies, put it behind an interface (extension point), add a class per variant; the core stays closed for modification.
- What is a race condition? Give an example. A result that depends on thread timing: two threads check "one copy left" and both take it.
- How do you make a class thread-safe? Identify shared mutable state; make compound actions atomic (
synchronized, locks, atomic classes,ConcurrentHashMap.compute), or avoid sharing (immutability, confinement). - Your design keeps data in a HashMap. How does it scale to many servers? Move state to a shared durable store behind the same repository interface; make updates atomic in the store.
- How do you justify a design pattern? Name the problem it solves today; otherwise say when you'd add it and why it's cheap later.
- What's over-engineering, and how do you avoid it? Structure without a requirement; follow YAGNI and the MVP, keep seams where change is likely.
Glossary
| Term | Meaning, with an example |
|---|---|
| Follow-up question | A new requirement or what-if after the code runs: "add late fines" |
| Extension point | A place built for new behaviour to plug in: FinePolicy |
| Seam | Same idea: where behaviour can be swapped without editing the code |
| Open-Closed Principle | Open for extension, closed for modification |
| Strategy | One interface, interchangeable algorithms: PerDay, NoFine |
| Decorator | Wraps an object of the same interface to add behaviour: Capped |
| Thread | An independent path of execution in one program |
| Race condition | A bug where the result depends on thread timing |
| Check-then-act | Checking a condition and acting on it as two separate steps |
| Thread-safe | Correct even when many threads call it at once |
| Shared mutable state | Data many threads can change: the copy count |
| Atomic | Happens all at once; nothing can run in between |
synchronized |
Lets only one thread at a time run a method or block on the same object |
| Lock per book | A separate lock for each item so unrelated work doesn't wait |
ConcurrentHashMap.compute |
Updates one map entry atomically |
| Load balancer | Sends each request to one of several servers |
| JVM | Java Virtual Machine: one running Java program; locks don't cross JVMs |
| RAM | A machine's working memory |
| Durable | Survives restarts and crashes |
| Single source of truth | The one store whose answer counts |
| Persistence | Keeping data beyond the program's life: files, databases |
| Persistence swap | Replacing the storage class behind a repository interface |
| Atomic conditional update | UPDATE ... SET copies = copies - 1 WHERE copies > 0 |
| Optimistic locking | Update only if the row's version is unchanged |
| Trade-off | Gaining one thing by giving up another |
| Over-engineering | Structure the problem doesn't need yet |
| YAGNI | "You aren't gonna need it": build only what's required |
| Design review | A meeting where engineers examine a design |
| Coupling / cohesion | Dependence between classes (keep low) / how well a class's parts belong together (keep high) |
| Invariant | A rule that must always be true: copies ≥ 0 |
Summary
- Follow-up questions test whether the design bends without breaking.
- New feature: find what varies, give it an extension point; a new rule is a new class (Open-Closed, Strategy, Decorator).
- Thread safety: find shared mutable state; make check-then-act one atomic step (
synchronized, per-key locks,ConcurrentHashMap.compute). - Scaling: memory isn't shared or durable; move to a single source of truth and make updates atomic in the store.
- Persistence swap: a new repository class; the service doesn't change.
- Explain trade-offs (X because Y, cost Z, switch to W if V), justify every pattern by the problem it solves, and say "not yet" to over-engineering.
Next episode: Design Tic-Tac-Toe, our first full machine-coding problem: board, cells, players, turns, and a win check in O(1).