2Brute2Best
πŸ”₯0
P8

Design a Library Management System

Medium Pro OnlyObserverDependency Inversion Principle (DIP)
⚑ 30-Second Interview Cheat SheetP8 · 45-Min Design Plan
3 Patterns Combined
1. Scope Boundaries

In a 45-minute LLD interview, the key architectural themes are domain modeling separation (the conceptual book title vs physical inventory copies), Dependency Inversion Principle (DIP) (injecting persistence and fine policy abstractions into LibraryService), and Observer-based event dispatching (notifying patrons on reservation fulfillment). Scope the solution to catalog management, loan tracking, overdue fine computation, FIFO hold queues, and observer notifications. Mention hold expiration timers (48-hour pickup window) and member borrowing limits as extensions.

2. Core Noun Entities
BookTitlestateful-class
BookCopystateful-class
CatalogRepositoryinterface
FineCalculatorinterface
3. Hotspot β†’ Pattern
Catalog persistence and database retrieval β†’ Dependency Inversion Principle (CatalogRepository interface)
Overdue fine assessment formulas β†’ Strategy / DIP (FineCalculator interface)
Patron notification on hold fulfillment β†’ Observer (ReservationObserver interface)
01

Problem statement & clarifying requirements

Design a Library Management System that manages catalog titles, physical barcoded book copies, member borrowing, hold reservations, and overdue fine calculations. The service layer must strictly adhere to the Dependency Inversion Principle (DIP) so that catalog persistence and fine calculation rules are injected as interfaces rather than hardcoded. When an overdue book is returned, the next member in the hold queue must be automatically alerted via the Observer pattern.

Questions you should ask the interviewer first
β€œWhat is the structural difference between a book title and a physical book copy?”

A BookTitle represents the intellectual work (ISBN, title, author, reservation hold queue), while a BookCopy represents a tangible inventory item with a unique barcode, rack location, and loan status.

β€œHow are reservations prioritized when all physical copies of a title are loaned out?”

Members join a FIFO hold queue attached to the BookTitle. When any copy of that ISBN is returned, it is immediately reserved for the next patron in line.

β€œCan another member borrow a copy while it is reserved for someone else?”

No. A copy with status Reserved can only be checked out by the member whose hold was fulfilled.

β€œHow are database storage and fine calculation algorithms decoupled from business logic?”

LibraryService depends exclusively on the CatalogRepository and FineCalculator abstract interfaces injected via constructor.

02

First, sketch the design yourself

Spend 5 minutes listing the classes, who owns whom, and which interface hides the part that changes.

  1. 1.

    Differentiate conceptual works from physical inventory: Modeling a book as a single class with both an ISBN and a single loan status breaks as soon as the library acquires 5 physical copies of the same book.

  2. 2.

    Invert high-level dependencies: Never let LibraryService instantiate SQL queries or a hardcoded fine formula. Depend on CatalogRepository and FineCalculator abstractions so fakes can be injected in unit tests.

  3. 3.

    Decouple reservation alerts with Observer: When a book copy is returned, the library service should not hardcode SMS or email logic. Publish a domain event to ReservationObserver subscribers.

03

How to think through this problem (5-step walkthrough)

Step 1 Β· Scope the 45-minute target & core boundary

In a 45-minute LLD interview, the key architectural themes are domain modeling separation (the conceptual book title vs physical inventory copies), Dependency Inversion Principle (DIP) (injecting persistence and fine policy abstractions into LibraryService), and Observer-based event dispatching (notifying patrons on reservation fulfillment). Scope the solution to catalog management, loan tracking, overdue fine computation, FIFO hold queues, and observer notifications. Mention hold expiration timers (48-hour pickup window) and member borrowing limits as extensions.

Step 2 Β· Extract candidate nouns (and reject false classes)

Not every noun in the prompt deserves its own class. Separate dumb data structs from stateful managers, and turn trivial nouns into fields or enums.

BookTitle
Stateful Class / Manager

Represents the intellectual work (ISBN, title, author) and manages the FIFO reservation hold queue (std::deque<std::string> holdQueue_).

BookCopy
Stateful Class / Manager

Represents a tangible physical item in stock with a unique barcode, rack location, CopyStatus (Available, Loaned, Reserved), and active loan metadata.

CatalogRepository
Abstract Interface

DIP persistence contract declaring methods to add/find titles and copies, decoupling LibraryService from concrete storage mechanisms.

FineCalculator
Abstract Interface

DIP strategy contract declaring calculateFinePaise(daysOverdue), allowing daily flat fees, progressive tiering, or holiday exemptions.

ReservationObserver
Abstract Interface

Observer contract receiving notifications when a reserved copy becomes available for a waiting patron.

LibraryService
Stateful Class / Manager

Core coordinator orchestrating borrowing, returns, holds, and fines through injected dependencies.

DueDate / BarcodeString
Reject as Class β†’ Field / Method

Trap noun β€” beginners often introduce dedicated DueDate, ReturnDate, or BarcodeString classes. A barcode is a primitive std::string, and due dates are cleanly modeled as integer day offsets or timestamps, avoiding gratuitous class bloat.

Step 3 Β· Draft the naive design & spot what breaks

A beginner merges book metadata and physical loan status into a single Book class (isbn, title, barcode, isBorrowed). When the library owns three physical copies of "Clean Code", the system must duplicate author and title data three times, and managing a single unified hold queue across all three copies becomes a tangled mess. Furthermore, hardcoding SQL queries or a fixed fine formula inside LibraryService makes unit testing impossible without an external database.

Step 4 Β· Map change hotspots to design patterns
Catalog persistence and database retrieval→ Dependency Inversion Principle (CatalogRepository interface)

Directly instantiating database clients inside LibraryService couples business logic to storage technology and prevents fast in-memory testing.

Overdue fine assessment formulas→ Strategy / DIP (FineCalculator interface)

Hardcoding fine calculation logic inside the return workflow breaks OCP when policies change (e.g. grace periods, member discounts, tiered penalties).

Patron notification on hold fulfillment→ Observer (ReservationObserver interface)

Hardcoding SMS or email sending inside returnBook() tightly couples loan management to external messaging services.

Step 5 Β· Lock down C++23 ownership, invariants & concurrency
  • βœ“

    Work vs Item cardinality invariant: A single BookTitle (identified by ISBN) can associate with zero or more physical BookCopy instances, but each BookCopy references exactly one valid ISBN.

  • βœ“

    Reservation lock invariant: When a book copy is marked CopyStatus::Reserved for patron M, no other patron may borrow that copy until M's reservation is fulfilled or expires.

  • βœ“

    Dependency Inversion: LibraryService depends exclusively on abstract interfaces (CatalogRepository, FineCalculator, ReservationObserver) injected via constructor, owning zero concrete database or notification instances.

04

Entities & design patterns used

Brute2Best Pro Content

Unlock Full UML Diagram & C++23 Solution

The complete UML diagram, production-ready C++23 code, follow-up extensions, and design pitfall analysis are part of Brute2Best Pro. All 7 foundation chapters are 100% free.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

Systems Integration

Related C++ Systems & Algorithmic Foundations

Deep dive into underlying layers