Identify Longest Episode - codepath/compsci_guides GitHub Wiki
Unit 4 Session 2 Standard (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 goal of the problem?
- A: The goal is to identify the longest episode duration from a given list of episode durations.
- Q: What are the inputs?
- A: The input is a list of integers where each integer represents the duration of a podcast episode in minutes.
- Q: What are the outputs?
- A: The output is a single integer representing the duration of the longest episode.
- Q: How should the function behave if there are multiple episodes with the same maximum duration?
- A: The function should simply return the maximum duration, even if it appears multiple times.
- Q: Are there any assumptions about the input?
- A: The list contains valid, non-negative integers representing the durations of episodes.
P-lan
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.
I-mplement
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