Design Library System (Low-Level Design)
Lend books to students and faculty with different limits, and charge fines for late returns. Focus: Policies that differ by member type.
| Difficulty | EASY |
|---|---|
| Design focus | Policies that differ by member type |
| Automated grading | 3 public + 10 hidden tests (13 total) |
| Phase 2 follow-up | Reservations |
| Language | TypeScript (OOP / Machine Coding) |
Problem statement
The library has books (by ISBN, with a number of copies) and members who are either STUDENT or FACULTY.
Students may hold 3 books at a time for 14 days; faculty may hold 10 books for 30 days. Days are whole numbers.
Returning late costs a fine per day late: students 10, faculty 5. A member with an unpaid fine cannot borrow until they pay it.
Functional requirements
- addBook(isbn, title, copies) adds copies (adding an existing ISBN adds more copies).
- addMember(id, name, kind) registers a member; borrow(memberId, isbn, day) lends a copy or throws (no copy, at the limit, unpaid fine, same book already held, unknown member or book).
- returnBook(memberId, isbn, day) returns the fine for that book (0 if on time) and adds it to the member's unpaid fines.
- fines(memberId), payFine(memberId), available(isbn) and borrowed(memberId) (ISBNs, in borrow order).
Required API & starter interface
Implement the TypeScript class interface below. You can add any helper classes, policies or strategies you need:
type MemberKind = "STUDENT" | "FACULTY";
class Library {
addBook(isbn: string, title: string, copies: number): void {
throw new Error("Not implemented");
}
addMember(id: string, name: string, kind: MemberKind): void {
throw new Error("Not implemented");
}
borrow(memberId: string, isbn: string, day: number): void {
throw new Error("Not implemented");
}
/** Returns the fine for this book */
returnBook(memberId: string, isbn: string, day: number): number {
throw new Error("Not implemented");
}
fines(memberId: string): number {
throw new Error("Not implemented");
}
payFine(memberId: string): void {
throw new Error("Not implemented");
}
available(isbn: string): number {
throw new Error("Not implemented");
}
borrowed(memberId: string): string[] {
throw new Error("Not implemented");
}
}
Public test cases
Your code runs against 3 public tests plus 10 hidden edge-case tests in a sandbox:
Borrow and return on time
var lib = new Library();
lib.addBook("111", "Clean Code", 1);
lib.addMember("s1", "Sam", "STUDENT");
lib.borrow("s1", "111", 0);
assertEqual(lib.available("111"), 0);
assertEqual(lib.returnBook("s1", "111", 14), 0);
assertEqual(lib.available("111"), 1);
Students pay 10 a day late
var lib = new Library();
lib.addBook("111", "Clean Code", 1);
lib.addMember("s1", "Sam", "STUDENT");
lib.borrow("s1", "111", 0);
assertEqual(lib.returnBook("s1", "111", 17), 30);
No copy left
var lib = new Library();
lib.addBook("111", "Clean Code", 1);
lib.addMember("a", "A", "STUDENT"); lib.addMember("b", "B", "STUDENT");
lib.borrow("a", "111", 0);
assertThrows(function () { lib.borrow("b", "111", 0); });
Design & structure checks
Beyond passing tests, the grader inspects your class structure for clean object-oriented design:
- Limits and fines come from a policy per member type, not if-statements on the kind: A third member type (staff, alumni) should mean adding one policy, not editing every method that checks the kind.
Phase 2: Change request
In real interviews, the follow-up tests whether your design is extensible. After your first version passes all 13 tests, you unlock the change request: Reservations.
Relevant low-level design lessons
From a problem to classes
Turn a problem statement into a first list of classes and methods.
Polymorphism at scale
Give each kind of thing its own class when the behaviour really differs.
Systems that move step by step
Model a system that changes over time with a clear step rule.
More low-level design problems
Design Snake and Ladder
A board game where the board, the players and the rules for a turn are separate pieces.
Design LRU Cache
A fixed-size cache that throws out the least recently used entry when full, with every operation in constant time.
Design Shopping Cart
Already solved, with comments: add items, apply tier discounts, then coupons. Run it and submit it to see how grading works.
Design Parking Lot
Park vehicles in the right size of spot, issue tickets and charge on exit.