Codepath

Identify Most Used Apps

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 the total time spent on each app (integers).
  • Q: What is the output?

    • A: The output is a string representing the name of the app with the highest total screen time.
  • Q: What should the function return if multiple apps have the same highest screen time?

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

    • A: It is assumed that the dictionary contains non-negative integer values for the time spent on each app.

P-lan

Plan the solution with appropriate visualizations and pseudocode.

General Idea: Iterate through the dictionary to find the app with the maximum total screen time.

1) Initialize two variables: `max_time` to -1 and `most_used` to `None`.
2) For each `app, time` in `screen_time.items()`:
   a) If `time` is greater than `max_time`, update `max_time` to `time` and `most_used` to `app`.
3) Return `most_used`.

**⚠️ Common Mistakes**

- Forgetting to handle the case where the input dictionary is empty (though it's assumed there is at least one app in the input).
- Not properly updating the `max_time` and `most_used` variables, leading to incorrect results.

I-mplement

def most_used_app(screen_time):
    max_time = -1
    most_used = None

    for app, time in screen_time.items():
        if time > max_time:
            max_time = time
            most_used = app

    return most_used
Fork me on GitHub