AlgoViz

Design a library management system

medium 85 min

Model a lending library: titles and copies, members with plans, loans, a waiting list and fines.

Video lesson · Chapter 1 of 6

Make a prediction

0:00 / 4:58

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

deskcallerlibraryLibraryloansnonelockfreeD1CopystatusON_SHELFkept fornobodyuntil-waitingWaitingList111nobodysamMemberplanregularloansnoneowes0

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

deskcallerlibraryLibraryloans5 loanslockfreeD1CopystatusON_SHELFkept fornobodyuntil-samMemberplanregularloans5 of 5owes0checkout(sam, D1)

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 loan

Common mistakes

The bugs interviewers watch for, with the fix beside each.

  • Status in the book

    critical

    With two copies there is no status of the book. The second loan overwrites the first.

    Wrong
    book.status = "BORROWED"book.borrowed_by = member
    Right
    copy.status = CopyStatus.ON_LOANloans[copy.barcode] = Loan(copy.barcode, member.id, member.plan, due)
  • Straight to the shelf

    critical

    Somebody who has waited for weeks is overtaken by whoever stands there.

    Wrong
    copy.status = CopyStatus.ON_SHELFnotify_queue(copy.isbn)
    Right
    news = self._pass_on(copy)   # queue first, then shelf
  • A lock for each book

    critical

    The limit belongs to the member. Two books, two locks, six of five.

    Wrong
    with book.lock:    if len(member.loans) < limit: ...
    Right
    with library.lock:    if len(member.loans) >= limit: raise Refused(Reason.LIMIT)
  • The plan of today

    major

    An upgrade at the counter erases the debt.

    Wrong
    fine = member.plan.fines.cents(days)
    Right
    fine = loan.plan.fines.cents(days)
  • Timer for a hold

    major

    Starts too early, forgotten on restart.

    Wrong
    scheduler.schedule(expire, 48, HOURS)
    Right
    copy.keep_until = today + timedelta(days=3)   # compared daily
  • A subclass for each kind

    minor

    Three numbers differ, and people change plan.

    Wrong
    class StudentMember(Member): max_loans = 3
    Right
    Member("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.