Unit 4 Session 2 Standard (Click for link to problem statements)
Understand what the interviewer is asking for by using test cases and questions about the problem.
Plan the solution with appropriate visualizations and pseudocode.
General Idea: Iterate through the list of durations, tracking the maximum value encountered. Return this maximum value as the result.
1) Initialize a variable `max_duration` to a very small number (e.g., 0).
2) Iterate through the `durations` list:
a) If the current duration is greater than `max_duration`, update `max_duration` to this value.
3) After iterating through the list, return `max_duration`, which now holds the longest episode duration.
**⚠️ Common Mistakes**
- Not initializing the `max_duration` correctly, which could lead to incorrect comparisons.
- Forgetting to handle edge cases such as an empty list or all durations being the same.
def identify_longest_episode(durations):
# Initialize the maximum duration to be a very small number
max_duration = 0
# Iterate over the list of durations
for duration in durations:
# Update the maximum duration if the current duration is greater
if duration > max_duration:
max_duration = duration
return max_duration
Example Usage:
print(identify_longest_episode([30, 45, 60, 45, 30]))
# Output: 60
print(identify_longest_episode([20, 30, 40, 40, 30, 20]))
# Output: 40
print(identify_longest_episode([55, 60, 55, 60, 60]))
# Output: 60