ayushsalampuriya.xyz Revise

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

2. Core entities

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

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.