THN Interview Prep

Design Library Management (LLD)

1. Requirements

  • Functional

    • Catalog books and copies; members borrow and return with due dates and fine policy.
    • Enforce max concurrent loans per member; place hold when all copies are out.
    • Librarian can add inventory, check in, and waive fines (audited).
  • Non-Functional

    • Consistent loan state: a copy cannot be both available and checked out.
    • Extensible fine rules and loan periods by book category or member tier.
  • Assumptions / Out of Scope

    • Digital lending and DRM omitted; physical copies only.

2. Core Entities

EntityResponsibilityKey Attributes
BookTitle metadataidentifier, title, author, isbn, category
BookCopyCirculation unitcopyId, bookId, status
MemberBorroweridentifier, name, activeLoans, tier
LoanCheckout recordcopy, member, start, due, status
HoldReservation queuemember, book, priority, createdAt
FinePolicyComputes finesrate per day, grace, caps
CatalogSearch and registerindex of books and copies

3. Class Diagram

Loading diagram…

4. State / Sequence Diagram (where relevant)

Loading diagram…

5. Design Patterns Applied

  • Factory — Create Loan with validated clock and due date. Factory Method pattern.
  • StrategyFinePolicy and LoanPeriodPolicy by category. Strategy pattern.
  • Repository (conceptual) — Catalog abstracts persistence; pair with Factory Method pattern for concrete stores when the pattern docs exist.

6. Implementation

Go

package library

type Book struct {
    Identifier, Title, ISBN string
}

type BookCopy struct {
    CopyID  string
    BookID  string
    Status  string
}

type Member struct {
    Identifier string
    Name       string
    ActiveLoanCount int
}

type Circulation struct {
    Catalog   *Catalog
    FineRules FinePolicy
}

func (circ *Circulation) Checkout(memberID, bookID string) (*Loan, error) { /* ... */ }
func (circ *Circulation) Return(copyID string) (fineCents int64, err error) { /* ... */ }

JavaScript

class Catalog {
  constructor() {
    this.booksById = new Map();
    this.copiesById = new Map();
  }
  addBook(book) { /* ... */ }
  addCopy({ copyId, bookId }) { /* ... */ }
}

class CirculationService {
  constructor({ catalog, finePolicy }) {
    this.catalog = catalog;
    this.finePolicy = finePolicy;
  }
  checkout({ memberId, bookId }) { /* ... */ }
  returnCopy({ copyId }) { /* ... */ }
}

7. Concurrency / Thread Safety

  • Collisions: Two clerks checking out last available copy; concurrent return and renew.
  • Granularity: Transaction wrapping copy status update and loan insert; DB unique constraint on active loan per copy.
  • Go: sync.Mutex per copy id in in-memory simulation.

8. Extensibility & Followups

  • Inter-library loan as workflow with external partner ids.
  • Notification scheduler on due soon and overdue.
  • Edge cases: lost book, damaged return, membership suspension.

Last updated on

Spotted something unclear or wrong on this page?

On this page