Design a Library Management System That Tracks Books, Members and Loans
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: easy
Interview Round: Other
Design a library management system that tracks books, members and borrowing transactions.
Implement a `LibrarySystem` class with these methods:
- `LibrarySystem() → void`: initialize an empty library system.
- `addBook(bookId: string, title: string, copies: number) → void`: add a book to the library. If the book already exists, increase its available copies.
- `registerMember(memberId: string, name: string) → boolean`: register a new member. Return `true` if successful, `false` if the member ID already exists.
- `borrowBook(memberId: string, bookId: string) → string`: attempt to borrow a book.
- If the member doesn't exist, return `"Member not found"`.
- If the book doesn't exist, return `"Book not found"`.
- If no copies are available, return `"No available copies"`.
- If the member already has this book, return `"Already borrowed"`.
- Otherwise, decrement the available copies, record the borrow and return `"Success"`.
- `returnBook(memberId: string, bookId: string) → string`: attempt to return a book.
- If the member doesn't exist, return `"Member not found"`.
- If the book doesn't exist, return `"Book not found"`.
- If the member hasn't borrowed this book, return `"Not borrowed"`.
- Otherwise, increment the available copies, remove the book from the member's borrowed list and return `"Success"`.
- `getAvailableCopies(bookId: string) → number`: return the available copies of a book, or `-1` if the book doesn't exist.
- `getBorrowedBooks(memberId: string) → string[]`: return the sorted list of book IDs the member currently has borrowed, or an empty array if the member doesn't exist.
The checks in `borrowBook` and `returnBook` apply in the order listed. The first one that matches decides the return value.
For example:
```text
Input:
["LibrarySystem", "addBook", "addBook", "registerMember", "borrowBook", "getAvailableCopies", "borrowBook", "returnBook", "getAvailableCopies"]
[[], ["B1", "Python Programming", 3], ["B2", "Data Structures", 2], ["M1", "Alice"], ["M1", "B1"], ["B1"], ["M1", "B2"], ["M1", "B1"], ["B1"]]
Output:
[null, null, null, true, "Success", 2, "Success", "Success", 3]
Explanation:
lib = LibrarySystem()
lib.addBook("B1", "Python Programming", 3) # Add 3 copies of B1
lib.addBook("B2", "Data Structures", 2) # Add 2 copies of B2
lib.registerMember("M1", "Alice") # → true (new member)
lib.borrowBook("M1", "B1") # → "Success" (B1 now has 2 available)
lib.getAvailableCopies("B1") # → 2
lib.borrowBook("M1", "B2") # → "Success" (B2 now has 1 available)
lib.returnBook("M1", "B1") # → "Success" (B1 now has 3 available)
lib.getAvailableCopies("B1") # → 3
```
```hint Lookups by ID
Every call names a member or a book by its ID. Pick structures that turn each check, including "does this member already hold this book?", into a constant-time lookup.
```
### Constraints & Assumptions
- `1 <= bookId.length, memberId.length, title.length, name.length <= 50`
- `1 <= copies <= 100`
- At most 10,000 total calls are made.
- A member cannot borrow more than one copy of the same book at the same time.
### Clarifying Questions to Ask
- If `addBook` is called again for an existing ID with a different title, should the title change or stay as first added?
- When a member already holds the last available copy and asks for it again, which message applies, `"No available copies"` or `"Already borrowed"`?
- Does "sorted" in `getBorrowedBooks` mean plain lexicographic order of the ID strings?
- Is there a limit on how many different books one member can hold at once?
- Do calls arrive from a single thread, or must the class be safe under concurrent access?
### What a Strong Answer Covers
- A data model that keeps the available copy count per book and the set of book IDs each member holds.
- Average constant-time checks for every borrow and return condition, evaluated in the order the prompt gives.
- Consistent state: the copy count and the member's borrowed set change together, and a failed call changes nothing.
- The edge cases: a repeated `addBook` for an existing ID, unknown members or books, and a `getBorrowedBooks` result that is sorted and empty for an unknown member.
- The time and space complexity of each operation.
### Follow-up Questions
- How would you add due dates and late fees, and how would you keep the time-dependent code testable?
- How would you add a waitlist so a returned copy is held for the next member in line?
- What changes if several librarians call `borrowBook` on the same book at the same moment?
- How would you let a member hold two copies of the same book, or cap how many books one member can hold?
Overview: Design a library system class that adds books, registers members, and handles borrow and return calls that return exact status messages. It tests choosing hash maps and sets for constant-time checks, ordering validation steps correctly, and keeping copy counts consistent after failed calls.