Design a library management system
medium 85 minModel a lending library: titles and copies, members with plans, loans, a waiting list and fines.
Video lesson · Chapter 1 of 6
Make a prediction
See it run
Objects at the counter: a copy moves between shelf, member and hold shelf, and the matching lines light up.
Operations
Checkout
All checks first, then three writes, under one lock.
Step 1 of 6
At the desk
Sam brings D1, the first copy of Dracula. It is on the shelf.
Step 1: At the desk
def checkout(self, member_id, barcode): with self._lock: member = self._known(self._members, member_id) copy = self._known(self._catalog.copies, barcode) self._may_borrow(member) if copy.status is CopyStatus.ON_LOAN: raise Refused(Reason.AWAY) if copy.status is CopyStatus.KEPT and copy.kept_for != member_id: raise Refused(Reason.KEPT) due = self._today() + timedelta(days=member.plan.days) loan = Loan(barcode, member_id, member.plan, due) copy.status, copy.kept_for, copy.keep_until = CopyStatus.ON_LOAN, None, None member.loans.add(barcode) self._loans[barcode] = loan return loan- Cost
- O(1)
- Guarantee
- One loan
- Scan of 200,000 copies
- for each beep
A refusal has changed nothing.
In the real world
The same library management doing real jobs. Pick one and step through it.
Five of five, and a helpful no.
Step 1 of 4
Five of five
Sam has five loans and brings D1.
Step 1: Five of five
def checkout(self, member_id, barcode): with self._lock: member = self._known(self._members, member_id) copy = self._known(self._catalog.copies, barcode) self._may_borrow(member) if copy.status is CopyStatus.ON_LOAN: raise Refused(Reason.AWAY) if copy.status is CopyStatus.KEPT and copy.kept_for != member_id: raise Refused(Reason.KEPT) due = self._today() + timedelta(days=member.plan.days) loan = Loan(barcode, member_id, member.plan, due) copy.status, copy.kept_for, copy.keep_until = CopyStatus.ON_LOAN, None, None member.loans.add(barcode) self._loans[barcode] = loan return loanCommon mistakes
The bugs interviewers watch for, with the fix beside each.
Status in the book
criticalWith two copies there is no status of the book. The second loan overwrites the first.
Wrongbook.status = "BORROWED"book.borrowed_by = memberRightcopy.status = CopyStatus.ON_LOANloans[copy.barcode] = Loan(copy.barcode, member.id, member.plan, due)Straight to the shelf
criticalSomebody who has waited for weeks is overtaken by whoever stands there.
Wrongcopy.status = CopyStatus.ON_SHELFnotify_queue(copy.isbn)Rightnews = self._pass_on(copy) # queue first, then shelfA lock for each book
criticalThe limit belongs to the member. Two books, two locks, six of five.
Wrongwith book.lock: if len(member.loans) < limit: ...Rightwith library.lock: if len(member.loans) >= limit: raise Refused(Reason.LIMIT)The plan of today
majorAn upgrade at the counter erases the debt.
Wrongfine = member.plan.fines.cents(days)Rightfine = loan.plan.fines.cents(days)Timer for a hold
majorStarts too early, forgotten on restart.
Wrongscheduler.schedule(expire, 48, HOURS)Rightcopy.keep_until = today + timedelta(days=3) # compared dailyA subclass for each kind
minorThree numbers differ, and people change plan.
Wrongclass StudentMember(Member): max_loans = 3RightMember("asha", plan=STUDENT)
Interview tips
Open with
- One book or three copies?
- Lend copies, wait for titles.
- The loan is a class.
Reject aloud
- Librarian is a role.
- Student is a plan.
- Overdue is calculated.
Prove it
- Trace a late return with a queue.
- Trace a lapse.
- Four of five at two counters.
Never
- Mail under the lock.
- A float for money.
- Two places that say who has it.