Library Management System
easy · CRUD + business rules
Brief-reported interview attribution (unverified): Amazon, Microsoft
How to approach an LLD roundDesign a library. It tracks books (copies available), members, and loans. A member may borrow at most 3 books at a time, and may not borrow the same book twice. A loan lasts 14 days; returning late costs $1 per day over 14.
class Library:
def __init__(self):
def add_book(self, book_id, title, author, copies):
# adding the same book_id again increases total and available copies
def register_member(self, member_id, name):
# registering the same member_id again is a no-op
def borrow(self, member_id, book_id):
# -> True if the loan is made, False otherwise
def return_book(self, member_id, book_id, days_borrowed):
# -> fine in dollars (int), or None if the member never borrowed that book
def available_copies(self, book_id):
# -> int (0 for an unknown book)
def borrowed_books(self, member_id):
# -> sorted list of book_ids the member currently has (empty for unknown member)
def search_by_title(self, query):
# -> sorted list of book_ids whose title contains the query (case-insensitive)
Rules. borrow returns False when the member or book is unknown, no copy is
available, the member already has 3 books, or the member already has this book.
return_book returns max(0, days_borrowed - 14) * 1.
Money units: late fees are whole dollars, not cents; six days late returns 6.
Tests use explicit inputs and injected clocks/callbacks. No network, real threads or application-server execution is involved.
Public demo
# Public demo: run after implementing the class API above.
l = Library()
for bid, title, copies in [('B1', 'Python Crash Course', 1), ('B2', 'Fluent Python', 2), ('B3', 'Clean Code', 1), ('B4', 'Design Patterns', 1)]:
l.add_book(bid, title, 'Author', copies)
l.register_member('M1', 'Asha')
l.register_member('M2', 'Ravi')
print(l.borrow('M1', 'B1'))
print(l.available_copies('B1'))Starter code
class Library:
def __init__(self):
"""Initialize the state described in the problem."""
pass
def add_book(self, book_id, title, author, copies):
"""adding the same book_id again increases total and available copies"""
pass
def register_member(self, member_id, name):
"""registering the same member_id again is a no-op"""
pass
def borrow(self, member_id, book_id):
"""-> True if the loan is made, False otherwise"""
pass
def return_book(self, member_id, book_id, days_borrowed):
"""-> fine in dollars (int), or None if the member never borrowed that book"""
pass
def available_copies(self, book_id):
"""-> int (0 for an unknown book)"""
pass
def borrowed_books(self, member_id):
"""-> sorted list of book_ids the member currently has (empty for unknown member)"""
pass
def search_by_title(self, query):
"""-> sorted list of book_ids whose title contains the query (case-insensitive)"""
pass
Each test has a five-second execution limit. Your practice drafts and solved progress stay in this browser.
Return to the Python compiler