Requirements
Functional Requirements
1. Book Management:
- Add, update, remove books
- Search by title, author, ISBN, genre
2. Member Management:
- Register, update, remove members
- Different member types (Student, Faculty, Guest)
3. Book Borrowing:
- Borrow books (max limit per member type)
- Return books
- Reserve books
4. Fine System:
- Calculate fine for late returns
- Different fine rates per member type
5. Notification:
- Due date reminders
- Reservation available notifications
- Fine notifications
Non-Functional Requirements
1. Concurrency: Multiple borrow/return simultaneously
2. Scalability: 100K+ books, 10K+ members
3. Search: Fast search across catalog
4. Availability: Real-time book availability
Core Entities
Book, BookItem, Member, Librarian,
BorrowingRecord, Reservation, Fine, Notification
Key Constraints
Student: Max 5 books, 14 days, $1/day fine
Faculty: Max 10 books, 30 days, $0.50/day fine
Guest: Max 2 books, 7 days, $2/day fine
Entity Design
Class Diagram
┌──────────────────────────────────────────────┐
│ Library (Singleton) │
├──────────────────────────────────────────────┤
│ - books: Catalog │
│ - members: List<Member> │
│ - librarians: List<Librarian> │
├──────────────────────────────────────────────┤
│ + addBook(book): void │
│ + searchBooks(query): List<BookItem> │
│ + borrowBook(member, book): BorrowingRecord │
│ + returnBook(record): Fine │
└──────────────────────────────────────────────┘
┌──────────────────────────────────────────────┐
│ BookItem │
├──────────────────────────────────────────────┤
│ - isbn: String │
│ - title: String │
│ - author: String │
│ - genre: Genre │
│ - status: BookStatus │
│ - rackLocation: String │
├──────────────────────────────────────────────┤
│ + isAvailable(): boolean │
│ + updateStatus(status): void │
└──────────────────────────────────────────────┘
┌──────────────────────────────────────────────┐
│ Member │
├──────────────────────────────────────────────┤
│ - memberId: String │
│ - name: String │
│ - type: MemberType │
│ - borrowedBooks: List<BorrowingRecord> │
│ - fines: List<Fine> │
├──────────────────────────────────────────────┤
│ + canBorrow(): boolean │
│ + getMaxBooks(): int │
│ + getMaxDays(): int │
└──────────────────────────────────────────────┘
Enums
public enum BookStatus {
AVAILABLE, BORROWED, RESERVED, LOST
}
public enum MemberType {
STUDENT, FACULTY, GUEST
}
public enum Genre {
FICTION, NON_FICTION, SCIENCE, HISTORY, TECHNOLOGY
}
Book Borrowing
Borrowing Flow
public class Library {
public BorrowingRecord borrowBook(String memberId, String isbn) {
// 1. Validate member
Member member = findMember(memberId);
if (!member.canBorrow()) {
throw new MaximumBorrowedException();
}
// 2. Find available book
BookItem book = catalog.findAvailableBook(isbn);
if (book == null) {
throw new BookNotAvailableException();
}
// 3. Create borrowing record
BorrowingRecord record = new BorrowingRecord(
member, book, LocalDateTime.now(),
LocalDateTime.now().plusDays(member.getMaxDays())
);
// 4. Update book status
book.updateStatus(BookStatus.BORROWED);
// 5. Add to member's borrowed books
member.addBorrowingRecord(record);
// 6. Schedule notification
notificationService.scheduleDueReminder(record);
return record;
}
}
Return Flow
public Fine returnBook(String recordId) {
// 1. Find record
BorrowingRecord record = findRecord(recordId);
// 2. Calculate fine if late
Fine fine = null;
if (LocalDateTime.now().isAfter(record.getDueDate())) {
long daysLate = Duration.between(
record.getDueDate(), LocalDateTime.now()
).toDays();
fine = new Fine(record.getMember(), daysLate,
calculateFineAmount(record.getMember().getType(), daysLate));
}
// 3. Update book status
record.getBook().updateStatus(BookStatus.AVAILABLE);
// 4. Mark record as returned
record.setReturnDate(LocalDateTime.now());
// 5. Check reservations
checkAndNotifyReservations(record.getBook());
return fine;
}
Reservation System
public class Reservation {
private final Member member;
private final BookItem book;
private final LocalDateTime reservationDate;
private ReservationStatus status;
}
// When book is returned, check reservations
private void checkAndNotifyReservations(BookItem book) {
List<Reservation> reservations = reservationService
.getReservationsForBook(book.getIsbn());
if (!reservations.isEmpty()) {
Reservation next = reservations.get(0);
book.updateStatus(BookStatus.RESERVED);
notificationService.notifyReservationAvailable(next);
}
}
Fine Calculation
public interface FineCalculator {
Money calculate(MemberType type, long daysLate);
}
public class StandardFineCalculator implements FineCalculator {
private static final Map<MemberType, Money> RATES = Map.of(
MemberType.STUDENT, new Money(1, "USD"),
MemberType.FACULTY, new Money(0.5, "USD"),
MemberType.GUEST, new Money(2, "USD")
);
public Money calculate(MemberType type, long daysLate) {
return RATES.get(type).multiply(daysLate);
}
}
Follow-ups
Follow-up Questions
1. How to handle concurrent borrow of same book?
→ Synchronized or database lock
→ Optimistic locking with version check
2. How to search books efficiently?
→ Inverted index for text search
→ Database indexing on ISBN, title, author
→ Elasticsearch for complex queries
3. How to handle multiple copies of same book?
→ Book (metadata) vs BookItem (physical copy)
→ Multiple BookItems per ISBN
4. How to handle inter-library loans?
→ LibraryNetwork manages multiple libraries
→ Transfer system between libraries
5. How to generate reports?
→ Most borrowed books
→ Member borrowing patterns
→ Overdue books report
Design Patterns
| Pattern | Usage |
|---|---|
| Singleton | Library |
| Strategy | Fine calculation, Search |
| Observer | Notifications |
| Factory | Book creation |
| State | Book status transitions |
Database Schema
books (isbn, title, author, genre)
book_items (id, isbn, status, rack)
members (id, name, type)
borrowing_records (id, member_id, book_id, borrow_date, due_date, return_date)
reservations (id, member_id, book_id, date, status)
fines (id, member_id, amount, paid)
Practice Problems
Design a scalable Library Management system. Cover high-level architecture, data model, and API design.
Solution
// Complete system design:
// - Functional + Non-functional requirements
// - Capacity estimation
// - Data model (SQL/NoSQL choice)
// - API endpoints
// - Component architecture
// - Scaling strategy
// - Monitoring & reliabilityHow would you scale Library Management to handle 10x the current load? Identify bottlenecks and solutions.
Solution
// Scaling approach:
// 1. Load balancing
// 2. Database sharding/replication
// 3. Cache layer (Redis)
// 4. CDN for static assets
// 5. Async processing (queues)
// 6. Microservices decompositionAnalyze potential failure modes for Library Management and design mitigation strategies.
Solution
// Failure mitigation:
// 1. Redundancy (multi-AZ)
// 2. Circuit breakers
// 3. Retry with backoff
// 4. Dead letter queues
// 5. Health checks
// 6. Graceful degradationQuiz
1. What is the difference between Book and BookItem?
2. How are different fine rates handled?
3. What happens when a reserved book is returned?
4. How is concurrency handled for borrowing the same book?
5. What pattern is used for notifications?
Flashcards
Question
Book vs BookItem?
Click to reveal answer
Answer
Book = metadata (ISBN, title, author). BookItem = physical copy (can be borrowed). One Book can have many BookItems.
Question
Borrowing flow steps?
Click to reveal answer
Answer
1) Validate member, 2) Find available book, 3) Create record, 4) Update status, 5) Add to member, 6) Schedule notification.
Question
Member type limits?
Click to reveal answer
Answer
Student: 5 books, 14 days, $1/day. Faculty: 10 books, 30 days, $0.50/day. Guest: 2 books, 7 days, $2/day.
Question
What pattern handles fine calculation?
Click to reveal answer
Answer
Strategy pattern with FineCalculator interface. Different implementations for Student, Faculty, Guest fine rates.
Question
How to handle book reservations?
Click to reveal answer
Answer
When reserved book is returned, status→RESERVED, first member in queue is notified. Reservation queue maintained per book.
Revision Notes
Key Takeaways
- 1.Book is metadata; BookItem is a physical copy that can be borrowed
- 2.Different member types have different borrowing limits and fine rates
- 3.Reservation system notifies members when reserved books become available
- 4.Concurrency handling is critical for simultaneous borrow attempts
- 5.Observer pattern handles notifications for due dates and reservations
Interview Tips
- •Distinguish between Book (metadata) and BookItem (physical copy)
- •Show how Strategy pattern enables flexible fine calculation
- •Explain concurrency handling for book availability
- •Discuss how reservations work with notification system
Cheat Sheet
Library Management - Cheat Sheet
Core Entities:
Book (metadata), BookItem (physical copy), Member, BorrowingRecord, Reservation, Fine
Borrowing Flow:
- Validate member (can borrow?)
- Find available book
- Create borrowing record
- Update book status
- Add to member's list
- Schedule due reminder
Return Flow:
- Find record
- Calculate fine if late
- Update book status
- Mark record returned
- Check/notify reservations
Member Limits:
| Type | Books | Days | Fine/Day |
|---|---|---|---|
| Student | 5 | 14 | $1 |
| Faculty | 10 | 30 | $0.50 |
| Guest | 2 | 7 | $2 |