Weekly App Usage - codepath/compsci_guides GitHub Wiki

Unit 4 Session 1 Advanced (Click for link to problem statements)

U-nderstand

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

  • Q: What is the structure of the input?

    • A: The input is a dictionary where the keys are app names (strings) and the values are lists of integers representing daily usage over seven days.
  • Q: What is the output?

    • A: The output is a string representing the name of the app with the highest difference between the maximum and minimum usage over the week.
  • Q: What should the function return if multiple apps have the same maximum difference?

    • A: The function should return any one of the apps with the highest difference.
  • Q: Are there any constraints on the input, such as the number of days?

    • A: It is assumed that the usage list for each app contains exactly seven integers.

P-lan

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).

I-mplement

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