Codepath

The Library of Alexandria

U-nderstand

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

  • Q
    • What is the desired outcome?
    • To determine the difference in the number of scrolls for each room between the expected and actual distributions.
    • What input is provided?
    • Two dictionaries, library_catalog and actual_distribution, mapping room names to the expected and actual number of scrolls respectively.

P-lan

Plan the solution with appropriate visualizations and pseudocode.

General Idea: Loop through the library_catalog and calculate the difference in scrolls for each room.

1) Initialize an empty dictionary `differences` to store the differences for each room.
2) Loop through the `library_catalog`:
   - For each room, retrieve the expected scrolls from `library_catalog`.
   - Retrieve the actual scrolls from `actual_distribution`, defaulting to 0 if the room is not present.
   - Calculate the difference and store it in `differences`.
3) Return the `differences` dictionary.

⚠️ Common Mistakes

  • Forgetting to handle rooms that are in library_catalog but missing in actual_distribution.

I-mplement

def analyze_library(library_catalog, actual_distribution):
    differences = {}
    
    # Loop over library_catalog to calculate the difference in scrolls for each room
    for room in library_catalog:
        expected_scrolls = library_catalog[room]
        actual_scrolls = actual_distribution.get(room, 0)
        differences[room] = actual_scrolls - expected_scrolls
    
    return differences
Fork me on GitHub