Unit 4 Session 1 Advanced (Click for link to problem statements)
Understand what the interviewer is asking for by using test cases and questions about the problem.
Q: What is the structure of the input?
"Monday"
, "Tuesday"
) and the value is another dictionary. This nested dictionary's keys represent app categories (e.g., "Social Media"
, "Entertainment"
) and its values represent the time spent (in minutes) on that category during that day.Q: What is the output?
"total_category_usage"
: a dictionary showing the total time spent on each category across the entire week."busiest_day"
: the day with the highest total usage."most_used_category"
: the category with the highest total usage over the week.Q: Are there any constraints on the input, such as valid day names?
Plan the solution with appropriate visualizations and pseudocode.
General Idea: Iterate through the data to compute the total time spent on each category, the total time spent on each day, and then determine the busiest day and most-used category.
1) Initialize an empty dictionary `total_category_usage` to store the total time spent on each category.
2) Initialize an empty dictionary `daily_totals` to store the total time spent each day.
3) Iterate through each `day, categories` in `weekly_usage.items()`:
a) Initialize `daily_total` to 0.
b) For each `category, time` in `categories.items()`:
- Add `time` to `daily_total`.
- Update `total_category_usage` by adding `time` to the corresponding category.
c) Store `daily_total` in `daily_totals` with the corresponding `day`.
4) Determine `busiest_day` by finding the key in `daily_totals` with the maximum value.
5) Determine `most_used_category` by finding the key in `total_category_usage` with the maximum value.
6) Return a dictionary containing `total_category_usage`, `busiest_day`, and `most_used_category`.
**⚠️ Common Mistakes**
- Not correctly summing the total time across categories or days.
- Misidentifying the busiest day or most-used category due to incorrect comparisons.
def analyze_weekly_usage(weekly_usage):
total_category_usage = {}
daily_totals = {}
for day, categories in weekly_usage.items():
daily_total = 0
for category, time in categories.items():
daily_total += time
if category in total_category_usage:
total_category_usage[category] += time
else:
total_category_usage[category] = time
daily_totals[day] = daily_total
busiest_day = max(daily_totals, key=daily_totals.get)
most_used_category = max(total_category_usage, key=total_category_usage.get)
return {
"total_category_usage": total_category_usage,
"busiest_day": busiest_day,
"most_used_category": most_used_category
}