FunctionRacerProblemsRace

Library Loans and Due Days

MediumClassesPython, JavaScript, TypeScript, Java, C++

Implement a class Library that lends books for a fixed number of days and keeps track of late returns. Days are whole numbers, and day never decreases from one call to the next. Write these methods:

The tests call the methods in sequence and compare every return value. The expected list holds null for the constructor.

Example 1

Library(7)
add_copies("dune", 1) returns 1
lend("ann", "dune", 0) returns 7
lend("bob", "dune", 1) returns -1
list_overdue(7) returns []
list_overdue(8) returns ["ann"]
give_back("ann", "dune", 10) returns 3
lend("bob", "dune", 10) returns 17
give_back("bob", "dune", 12) returns 0
give_back("bob", "dune", 12) returns -1

ann borrows the only copy on day 0, due on day 7, so bob cannot borrow it. On day 7 the book is due but not yet overdue; on day 8 it is. It comes back on day 10, three days late. bob then borrows it and returns it early, and a second return finds no loan.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Classes problems