Skip to content

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)

U-nderstand

Understand what the interviewer is asking for by using test cases and questions about the problem.

  • What are the inputs?
    • The title of the returned book, and a dictionary library that maps each title to the number of copies on the shelf.
  • What should the function return?
    • The library dictionary, updated in place with one more copy of title.
  • 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.

P-lan

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 dict

I-mplement

def return_book(title, library):
    if title in library:
        library[title] += 1
    else:
        library[title] = 1
    return library

Clone this wiki locally