LLD practice: mock rounds
60 min · Advanced
Need help? Ask a tutor on WhatsApp (opens in a new tab)Reading designs does not prepare you for a clock. These mock rounds do: design from scratch, find flaws in a weak design, extend a finished one, drill the first minutes and survive "what if" questions.
Timed mock: library management system
A timed mock is a practice round with the real 45-minute clock and the same steps as an interview.
The problem: a college library lends books to members. A member can hold at most 3 books, a loan is free for 14 days, and each late day costs Rs 2.
Example 1 · Mock answer: the library core flow
import java.time.LocalDate;import java.time.temporal.ChronoUnit;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;
record Book(String isbn, String title) { }
record Member(String id, String name) { }
record Loan(Book book, Member member, LocalDate issuedOn) { }
interface FinePolicy { int fine(long daysKept);}
class PerDayAfterFreeDays implements FinePolicy { public int fine(long daysKept) { return (int) Math.max(0, daysKept - 14) * 2; }}
class Library { private static final int MAX_BOOKS = 3; private final Map<String, Book> books = new HashMap<>(); private final Map<String, Integer> freeCopies = new HashMap<>(); private final List<Loan> openLoans = new ArrayList<>(); private final FinePolicy fines;
Library(FinePolicy fines) { this.fines = fines; }
void addBook(Book book, int copies) { books.put(book.isbn(), book); freeCopies.merge(book.isbn(), copies, Integer::sum); }
synchronized String issue(Member m, String isbn, LocalDate on) { Book book = books.get(isbn); long held = openLoans.stream().filter(l -> l.member().equals(m)).count(); if (held >= MAX_BOOKS) return m.name() + " already has " + MAX_BOOKS + " books"; if (freeCopies.getOrDefault(isbn, 0) == 0) return book.title() + ": no free copy"; freeCopies.merge(isbn, -1, Integer::sum); openLoans.add(new Loan(book, m, on)); return m.name() + " took " + book.title(); }
synchronized String giveBack(Member m, String isbn, LocalDate on) { Loan loan = openLoans.stream() .filter(l -> l.member().equals(m) && l.book().isbn().equals(isbn)) .findFirst().orElseThrow(); openLoans.remove(loan); freeCopies.merge(isbn, 1, Integer::sum); long days = ChronoUnit.DAYS.between(loan.issuedOn(), on); return m.name() + " returned " + loan.book().title() + " after " + days + " days, fine Rs " + fines.fine(days); }}
public class LibraryMockDemo { public static void main(String[] args) { Library lib = new Library(new PerDayAfterFreeDays()); lib.addBook(new Book("111", "Let Us C"), 1); lib.addBook(new Book("222", "Head First Java"), 2); lib.addBook(new Book("333", "Wings of Fire"), 1); lib.addBook(new Book("444", "Godan"), 1); Member asha = new Member("M1", "Asha"); Member ravi = new Member("M2", "Ravi"); LocalDate jul1 = LocalDate.of(2026, 7, 1);
System.out.println(lib.issue(asha, "111", jul1)); System.out.println(lib.issue(ravi, "111", jul1)); System.out.println(lib.issue(asha, "222", jul1)); System.out.println(lib.issue(asha, "333", jul1)); System.out.println(lib.issue(asha, "444", jul1)); System.out.println(lib.giveBack(asha, "111", jul1.plusDays(10))); System.out.println(lib.giveBack(asha, "222", jul1.plusDays(20))); System.out.println(lib.issue(ravi, "111", jul1.plusDays(20))); }}Asha took Let Us C Let Us C: no free copy Asha took Head First Java Asha took Wings of Fire Asha already has 3 books Asha returned Let Us C after 10 days, fine Rs 0 Asha returned Head First Java after 20 days, fine Rs 12 Ravi took Let Us C
What this code does
- Four entities and one strategy cover the agreed scope; the fine rule is one line in
PerDayAfterFreeDays, so "Rs 5 a day in exam week" is a new class. issuechecks the 3-book limit and a free copy before it changes anything, and issynchronizedbecause two counters could issue the last copy at once.- A 20-day loan is 6 days late at Rs 2, so the fine is Rs 12. Once Asha returns Let Us C, Ravi can take it.
Spend 5 minutes on scope (copies per book? reservations? fines paid now or later?), 5 on entities (Book, Member, Loan, Library), 5 on relationships and the fine rule as a FinePolicy strategy, and the rest on a running core flow: issue, return with fine, and the 3-book limit.
The plan for this problem:
- Entities:
Book(isbn, title, copies),Member(id, name),Loan(book, member, issued date). - Rules: at most 3 open loans per member; no issue when no copy is free.
- Strategy:
FinePolicyturns days kept into rupees, so exam-week rules can change it. - Out of scope for the first pass: reservations, search, payment of fines.
Order of execution
Run the 45 minutes
Put the parts of the mock round in the order you would do them.
Find design flaws in a given parking lot solution
A design review is reading a design for flaws and their effects, then fixing each flaw with the smallest change.
A parking lot that looks fine hides three bugs. Find them, then see the lot after the review.
Example 2 · A weak parking lot and its bugs
class ParkingLot { String[] spotSize = {"small", "large"}; String[] parkedAt = new String[2];
// parks, prices, prints: one class does everything int park(String number, String type) { for (int i = 0; i < parkedAt.length; i++) { if (parkedAt[i] == null) { // size is never checked parkedAt[i] = number; return i; } } return -1; }
int fee(String type, int hours) { if (type.equals("car")) return hours * 40; else if (type.equals("bike")) return hours * 20; return 0; // trucks and typos are free }}
public class WeakParkingDemo { public static void main(String[] args) { ParkingLot lot = new ParkingLot(); int spot = lot.park("KA01TR3333", "truck"); System.out.println("Truck parked in a " + lot.spotSize[spot] + " spot"); System.out.println("Truck fee for 3 hours: Rs " + lot.fee("truck", 3)); System.out.println("Car fee typed as \"Car\": Rs " + lot.fee("Car", 3)); }}Truck parked in a small spot Truck fee for 3 hours: Rs 0 Car fee typed as "Car": Rs 0
What this code does
- Flaw 1: types are strings, so
"truck"and the typo"Car"fall through theifchain and pay Rs 0. - Flaw 2:
parknever checks size, so a truck takes a small spot. Flaw 3: fields are public arrays, there is no lock, and pricing, parking and data all live in one class. - Each flaw is a real bug you can show with output, which makes the review concrete.
Example 3 · The same lot after the review
import java.util.List;import java.util.Map;
enum VehicleType { BIKE, CAR, TRUCK }enum SpotSize { SMALL, LARGE }
class Spot { final SpotSize size; private String parked; Spot(SpotSize size) { this.size = size; } boolean canFit(VehicleType type) { return parked == null && (type != VehicleType.TRUCK || size == SpotSize.LARGE); } void park(String number) { parked = number; }}
interface FeeStrategy { int fee(VehicleType type, int hours); }
class HourlyFee implements FeeStrategy { private final Map<VehicleType, Integer> rate = Map.of(VehicleType.BIKE, 20, VehicleType.CAR, 40, VehicleType.TRUCK, 100); public int fee(VehicleType type, int hours) { return hours * rate.get(type); }}
class ParkingLot { private final List<Spot> spots; ParkingLot(List<Spot> spots) { this.spots = spots; }
synchronized String park(String number, VehicleType type) { for (Spot s : spots) { if (s.canFit(type)) { s.park(number); return type + " parked in a " + s.size + " spot"; } } return type + ": no spot that fits"; }}
public class ReviewedParkingDemo { public static void main(String[] args) { ParkingLot lot = new ParkingLot(List.of(new Spot(SpotSize.SMALL), new Spot(SpotSize.LARGE))); FeeStrategy fees = new HourlyFee(); System.out.println(lot.park("KA01TR3333", VehicleType.TRUCK)); System.out.println("Truck fee for 3 hours: Rs " + fees.fee(VehicleType.TRUCK, 3)); System.out.println("Car fee for 3 hours: Rs " + fees.fee(VehicleType.CAR, 3)); System.out.println(lot.park("KA01TR4444", VehicleType.TRUCK)); }}TRUCK parked in a LARGE spot Truck fee for 3 hours: Rs 300 Car fee for 3 hours: Rs 120 TRUCK: no spot that fits
What this code does
- An enum makes a typo a compile error, and the rate map has a value for every type, so no vehicle is free by accident.
- The fit check lives in
Spot, the fee in a strategy and parking in asynchronizedmethod: three jobs, three places. - The second truck is refused because the only large spot is taken, which the old code would have "solved" with a small spot.
Read it against a short checklist: does each class have one job, are types modelled as strings, are rules spread around, is shared state safe across threads, and does a new requirement mean editing old code?
Say each flaw with its effect, not just its name: "vehicle types are strings, so a typo like "Car" silently pays Rs 0" is better than "should use an enum". Then show the smallest fix.
Find the bug
Name the flaw
Click the line with the flaw.
Fees are wrong for some vehicles.
Round 1 of 2Extend the booking design with coupons
An extension round gives you an earlier design and a new requirement to add without breaking the old behaviour.
Coupons are added to the booking design: FLAT50 (Rs 50 off orders of Rs 300 or more) and MOVIE20 (20% off, at most Rs 120), each with an expiry date.
Example 4 · Coupons added to the booking design
import java.time.LocalDate;import java.util.List;
abstract class Coupon { final String code; private final int minAmount; private final LocalDate validTill;
Coupon(String code, int minAmount, LocalDate validTill) { this.code = code; this.minAmount = minAmount; this.validTill = validTill; }
// shared checks live here once final String check(int amount, LocalDate on) { if (on.isAfter(validTill)) return "expired on " + validTill; if (amount < minAmount) return "needs an order of Rs " + minAmount; return null; }
abstract int discount(int amount);}
class FlatOff extends Coupon { private final int off; FlatOff(String code, int off, int min, LocalDate till) { super(code, min, till); this.off = off; } int discount(int amount) { return off; }}
class PercentOff extends Coupon { private final int percent, cap; PercentOff(String code, int percent, int cap, int min, LocalDate till) { super(code, min, till); this.percent = percent; this.cap = cap; } int discount(int amount) { return Math.min(amount * percent / 100, cap); }}
class Checkout { static String pay(int amount, Coupon coupon, LocalDate on) { String problem = coupon.check(amount, on); if (problem != null) return coupon.code + " on Rs " + amount + ": not applied, " + problem; int off = coupon.discount(amount); return coupon.code + " on Rs " + amount + ": Rs " + off + " off, pay Rs " + (amount - off); }}
public class CouponDemo { public static void main(String[] args) { LocalDate till = LocalDate.of(2026, 10, 31); Coupon flat = new FlatOff("FLAT50", 50, 300, till); Coupon movie = new PercentOff("MOVIE20", 20, 120, 0, till); LocalDate today = LocalDate.of(2026, 10, 18);
for (Coupon c : List.of(flat, movie)) { System.out.println(Checkout.pay(440, c, today)); System.out.println(Checkout.pay(900, c, today)); } System.out.println(Checkout.pay(250, flat, today)); System.out.println(Checkout.pay(440, movie, LocalDate.of(2026, 11, 2))); }}FLAT50 on Rs 440: Rs 50 off, pay Rs 390 FLAT50 on Rs 900: Rs 50 off, pay Rs 850 MOVIE20 on Rs 440: Rs 88 off, pay Rs 352 MOVIE20 on Rs 900: Rs 120 off, pay Rs 780 FLAT50 on Rs 250: not applied, needs an order of Rs 300 MOVIE20 on Rs 440: not applied, expired on 2026-10-31
What this code does
- The base class checks expiry and the minimum amount once;
FlatOffandPercentOffonly say how much to take off. MOVIE20gives 20% of 900 = 180, but its cap limits it to Rs 120; on Rs 440 it gives 88.Checkoutnever tests coupon codes, so a "first booking free popcorn" coupon would be one more subclass.
Model a coupon as its own small type with a common base: the base checks expiry and minimum amount once, and each coupon only computes its discount.
The booking service asks the coupon for a discount and never needs an if per coupon code.
Predict the output
Predict the MOVIE20 price
MOVIE20 is 20% off, at most Rs 120.
Round 1 of 2
Short drills: entities and relationships for five systems
An entity drill is a two-minute practice of the first ten minutes of a round: list the main entities, one key relationship and the rule that needs the most care.
Five systems, two minutes each: entities, one relationship and the rule that must never fail.
Railway reservation: Train, Coach, Berth, Passenger, Ticket(PNR) | Train 1-* Coach 1-* Berth | never sell one berth twice per journeyHospital appointments: Doctor, Slot, Patient, Appointment | Doctor 1-* Slot, Slot 0..1 Appointment | one patient per slotCollege fest registration: Event, Team, Student, Registration | Event 1-* Registration | team size limits, no duplicate studentOnline store: Customer, Product, Cart, Order, DeliveryAgent | Order 1-* OrderLine, Order 0..1 Agent | one agent per orderHotel booking: Hotel, RoomType, Room, Booking, Guest | Room 1-* Booking | no overlapping dates per roomSay each answer out loud. The point is speed and the habit of asking "what must never go wrong?", because that answer usually decides where locks, states or validation go.
Interview drill: handling "what if" questions
A follow-up question is a "what if" from the interviewer, such as "what if two people book at once?", that tests whether your design bends or breaks.
A strong answer to a "what if" has three parts: which class changes, what the change is, and what stays untouched. Typical follow-ups and the design move each one needs:
- Concurrency ("two users at once") → lock the check-and-update step, or use an atomic map operation.
- New variant ("add UPI", "add trucks") → a new class behind an existing interface.
- New rule on a price or a fine → a new strategy, or a wrapper around the old one.
- New lifecycle step ("add refunds") → a new state and its allowed moves.
If the honest answer is "this needs a bigger change", say so and explain why; interviewers trust that more than a claim that nothing changes.
Next step
Check your understanding
12 quick questions