LLD · Classic
Library Management System
Design classes to search books, lend copies to members, track due dates, and handle returns and fines.
Simple summary: A library tracks book copies and who borrowed
what. Lending marks a copy as unavailable; returning frees it for the next member.
Example: Member M12 borrows copy C-101 of "Clean Code" due in 14 days;
on return, fine is zero if on time.
1. Clarify requirements
- Catalog: title, author, ISBN; multiple physical copies per title.
- Members can borrow up to N books at once.
- Loan period (e.g. 14 days); optional fine per day late.
- Search by title or author; reserve if all copies out (optional).
- Out of scope: e-books, inter-library shipping network.
2. Core entities
- BookTitle — isbn, title, author.
- BookCopy — copyId, isbn, status (AVAILABLE, LOANED, LOST).
- Member — memberId, name, activeLoans list.
- Loan — loanId, copyId, memberId, borrowedAt, dueAt, returnedAt.
- Library — search, checkout, return, computeFine.
3. Checkout flow
checkout(memberId, copyId):
if member.activeLoans.size >= MAX_LOANS: reject
copy = findCopy(copyId)
if copy.status != AVAILABLE: reject
copy.status = LOANED
loan = new Loan(copyId, memberId, now(), now()+14days)
member.activeLoans.add(loan)
return loan
4. Return flow
returnCopy(loanId):
loan = findLoan(loanId)
loan.returnedAt = now()
copy.status = AVAILABLE
fine = computeFine(loan.dueAt, loan.returnedAt)
member.activeLoans.remove(loan)
return fine
Fine example: due Jan 1, returned Jan 4 → 3 days late × ₹5/day = ₹15.
5. Search
Index titles by author and title keywords (hash map or simple in-memory filter for interview scale). Return list of available copy counts per ISBN.
6. Concurrency
Two clerks must not loan the same copy. Lock per copyId or atomic status check: only one checkout succeeds when status is AVAILABLE.
7. Interview extensions
- Reservation queue per ISBN when all copies loaned.
- Renewal if no reservations and under renewal limit.
- Librarian role vs member role for admin APIs.
Quick revision
- Separate BookTitle (catalog) from BookCopy (physical item).
- Checkout: verify limits + copy AVAILABLE → create Loan.
- Return: free copy, compute fine from due date.
- Concurrency: one copy → one active loan.