Understand what the interviewer is asking for by using test cases and questions about the problem.
library_catalog
and actual_distribution
, mapping room names to the expected and actual number of scrolls respectively.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
library_catalog
but missing in actual_distribution
.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