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 highest screen time?
Q: Are there any constraints on the input, such as the number of apps or time values?
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.
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