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?
Q: What is the output?
Q: What should the function return if multiple apps have the same maximum difference?
Q: Are there any constraints on the input, such as the number of days?
Plan the solution with appropriate visualizations and pseudocode.
General Idea: Iterate through each app's usage data, calculate the difference between the maximum and minimum usage for the week, and track the app with the largest difference.
1) Initialize two variables: `max_difference` to -1 and `varied_app` to `None`.
2) For each `app, usage` in `app_usage.items()`:
a) Calculate the difference between `max(usage)` and `min(usage)`.
b) If this difference is greater than `max_difference`, update `max_difference` to this value and `varied_app` to `app`.
3) Return `varied_app`.
**⚠️ Common Mistakes**
- Forgetting to correctly calculate the difference for each app.
- Not handling the case where multiple apps have the same maximum difference (though any one of them is acceptable).
def most_varied_app(app_usage):
max_difference = -1
varied_app = None
for app, usage in app_usage.items():
difference = max(usage) - min(usage)
if difference > max_difference:
max_difference = difference
varied_app = app
return varied_app