Library Management System

Book Lending Platform

Design a library: a catalog of books with physical copies, members with lending rules, loans with due dates and fines, and a hold queue that offers a returned copy to the next member in line.

Overview

A library lends physical copies of books to members for a fixed period, charges a fine when a copy comes back late, and lets a member wait in line for a book whose copies are all out. The domain is familiar, which is why interviewers use it: there is no algorithm to hide behind, and the quality of the object model is the whole answer.

The problem tests three modeling decisions. First, a book is not a copy: the catalog entry is one object, each physical copy is another, and only copies are lent. Second, lending rules belong in a policy object attached to the member, not in a chain of if member.type == ... checks. Third, a returned copy does not simply become available; if members are waiting, it is held for the first of them until a deadline, and the checkout code has to respect that hold.

A strong answer draws the entities and the copy's life cycle before writing code, passes the current date into the library instead of reading the clock inside it so that due dates and fines are testable, and puts one lock around checkout and return so that two members can never borrow the same copy.

Scoping the problem

Ask six questions before you model anything. Each one decides a piece of the design, and the usual answer follows it.

  • Can a book have several copies? Usually yes. This splits Book from BookCopy, the key modeling step.
  • Do member types differ? Yes, at least two kinds. Loan limit, loan length and fine rate then become a policy per member.
  • What happens when every copy is out? Members join a hold queue per book, first come first served, with a pickup deadline.
  • How are fines charged? Per day late, from the policy, capped or not by choice. Also settle whether a fine must be paid before the next checkout.
  • Who calls the system? Both a librarian at a desk and members online. That changes the concurrency story, so the library needs a lock.
  • Search, notifications, renewals, lost copies? Each is a follow-up, so ask which the interviewer wants. The usual answer is notifications and one more.

Functional requirements

  • Catalog books by ISBN and register any number of physical copies per book
  • Register members, each with a policy: maximum open loans, loan length in days, fine per day late
  • Check out an available copy to an eligible member and record the due date
  • Return a copy, compute the fine for late days, and close the loan
  • Place a hold on a book whose copies are all out; a returned copy is held for the first member in line until a pickup deadline
  • Expire holds whose deadline passed and pass the copy to the next member or back to the shelf
  • Notify a member when a held copy is ready

Non-functional requirements

  • A copy is never on loan to two members: checkout and return are atomic under one lock
  • Dates come from the caller, never from the system clock, so due dates and fines are testable
  • Fines are integers in cents
  • Lookups by copy id, ISBN and member id are O(1) via dictionaries
  • Adding a member type or a notification channel needs no change to checkout or return

Interview questions

  • Why not a Book class with a count of copies? Why a BookCopy object per item?
  • How do you support member types with different rules without an if-chain?
  • Design the hold queue. What goes wrong with the simple version?
  • How does this survive many desks and a database? What stops two checkouts of one copy?
  • What would you add next: renewals, lost copies, search?

Sign up free to read the full topic

More Low-Level Design topics

Start free on Cariara · Coding problems · System design studio