KatInterview

Library Loan Alert Engine: Holds, Checkouts, and Due Reminders

Data Structure DesignModel answer
MediumModel answerData Structure Designheapsimulationidempotency

Last updated

Design a notification manager for a book lending system that handles hold queues, checkout allocation, and idempotent due-date alerts.

Library Loan Alert Engine

A digital lending platform tracks physical book copies, patron loans, and waiting lists. Your job is to build the LendingAlertManager class that processes lending events and time-based polling to send patrons the right notification at the right time — and never twice.

What to implement


add_to_waitlist(patron_id: str, book_id: str, timestamp: int) -> bool

Registers a patron as waiting for any available copy of book_id.

  • A patron may only appear once on a book's waitlist at a time. Return True on success, False if they are already waiting (or already have a pickup ready).

process_return(copy_id: str, book_id: str, timestamp: int) -> dict | None

Records that a copy has come back.

  • If anyone is on the waitlist for book_id, assign the copy to the patron who has been waiting the longest (earliest timestamp). Mark it READY with a 48-hour pickup window: expires_at = timestamp + 172800. Return a notification:
{
    "type": "HOLD_AVAILABLE",
    "patron_id": str,
    "book_id": str,
    "copy_id": str,
    "expires_at": int,
    "timestamp": int   # the return timestamp
}
  • If the waitlist is empty, park the copy in general inventory and return None.

borrow(patron_id: str, copy_id: str, book_id: str, timestamp: int, loan_days: int = 14) -> bool

Lends a copy to a patron.

  • Succeeds in exactly two situations:
    1. The copy is currently held READY specifically for this patron (consuming that reservation).
    2. The copy is sitting freely in general inventory under the same book_id.
  • On success, record a loan with due_date = timestamp + loan_days * 86400.
  • Return False in all other cases (copy on loan, reserved for someone else, unknown copy, mismatched book).

poll_reminders(current_time: int) -> list[dict]

Scans every active loan and returns applicable alerts.

Due Soon — emit once per loan when: 0 < due_date - current_time <= 86400

Overdue — once the loan is past due (current_time > due_date), emit one alert per 86,400-second window. The first overdue alert fires as soon as current_time > due_date. Each subsequent alert requires at least 86,400 seconds to have elapsed since the previous one was sent for that loan.

Notification format for both types:

{
    "type": "LOAN_DUE_SOON" | "LOAN_OVERDUE",
    "patron_id": str,
    "copy_id": str,
    "due_date": int,
    "timestamp": current_time
}

Idempotency: calling poll_reminders twice with the same current_time must produce an empty list on the second call.


Constraints

  • All timestamps are Unix epoch seconds (integers).
  • At most 10,000 total method calls.
  • A copy belongs to exactly one patron at a time.
  • Tie-breaking on the waitlist: when two patrons joined at the same timestamp, the one who called add_to_waitlist first wins.

Example

mgr = LendingAlertManager()

mgr.add_to_waitlist("p1", "book_A", 1000)
mgr.add_to_waitlist("p2", "book_A", 1050)

notif = mgr.process_return("copy_1", "book_A", 2000)
# notif == {
#     "type": "HOLD_AVAILABLE",
#     "patron_id": "p1",
#     "book_id": "book_A",
#     "copy_id": "copy_1",
#     "expires_at": 174800,   # 2000 + 172800
#     "timestamp": 2000
# }

assert mgr.borrow("p1", "copy_1", "book_A", 2500, loan_days=2)
# due_date = 2500 + 2 * 86400 = 175300

# 30 hours after checkout: current_time = 2500 + 108000 = 110500
# time until due: 175300 - 110500 = 64800 seconds (~18 h) → DUE SOON
reminders = mgr.poll_reminders(110500)
assert len(reminders) == 1 and reminders[0]["type"] == "LOAN_DUE_SOON"

# Same timestamp → idempotent, no duplicates
assert mgr.poll_reminders(110500) == []

Follow-up questions to consider:

  • How would you handle a patron who never picks up their reserved copy, freeing it for the next person in line?
  • How would you scale this to millions of active loans without scanning every loan on every poll_reminders call?

Hints

Hint 1

There are four distinct places a copy can live: general inventory, a named reservation, an active loan, or nowhere yet. Model each as a separate data structure and make sure a copy is in exactly one at any given moment.

Hint 2

The waitlist needs earliest-first ordering with arrival-order tie-breaking. A min-heap of (timestamp, sequence_number, patron_id) gives you both properties in O(log n) per push/pop.

Hint 3

For idempotent reminders, track two things per loan: a boolean that flips once when the due-soon alert fires, and the timestamp of the last overdue alert sent. A second call at the same time produces a difference of zero, which fails the ≥ 86400 check automatically.