Repository navigation
Return Book
codepath-wiki-review[bot] edited this page Nov 20, 2026
·
6 revisions
TIP101 Unit 2 Session 2 (Click for link to problem statements)
Understand what the interviewer is asking for by using test cases and questions about the problem.
- What are the inputs?
- The
titleof the returned book, and a dictionarylibrarythat maps each title to the number of copies on the shelf.
- The
- What should the function return?
- The
librarydictionary, updated in place with one more copy oftitle.
- The
- What if the title is not in the library yet?
- Add it to the dictionary with 1 copy.
HAPPY CASE
Input: title = "Dune", library = {"Dune": 2, "Emma": 1}
Output: {"Dune": 3, "Emma": 1}
Explanation: The library already has 2 copies of "Dune", so the count goes up to 3.
EDGE CASE
Input: title = "Dune", library = {"Emma": 1}
Output: {"Emma": 1, "Dune": 1}
Explanation: "Dune" is not in the library yet, so it is added with 1 copy.
Plan the solution with appropriate visualizations and pseudocode.
General Idea: Check if the book is already in the dictionary, add them if not, then increment their quantity by 1.
1) If the book is a key in the dict, increment the value by 1
2) Otherwise, map book -> 1 in the dictdef return_book(title, library):
if title in library:
library[title] += 1
else:
library[title] = 1
return library