TIP102 Unit 3 Session 1 Standard (Click for link to problem statements)
Understand what the interviewer is asking for by using test cases and questions about the problem.
view_counts
of n
integers, where n
is even.Plan the solution with appropriate visualizations and pseudocode.
General Idea: Sort the array and use two pointers to repeatedly find the smallest and largest view counts, calculate their average, and track the minimum average.
1. Sort the `view_counts` array in ascending order.
2. Initialize an empty list `average_views` to store the calculated averages.
3. Initialize two pointers: `left` at the start of the array and `right` at the end.
4. Iterate `n/2` times:
1. Calculate the average of the elements at `left` and `right`.
2. Append the average to the `average_views` list.
3. Move the `left` pointer one step to the right and the `right` pointer one step to the left.
5. Return the minimum value from the `average_views` list.
⚠️ Common Mistakes
def minimum_average_view_count(view_counts):
# Sort the view counts in ascending order
view_counts.sort()
# Initialize an empty list to store the average views
average_views = []
# Initialize two pointers: one at the start (min) and one at the end (max) of the list
left = 0
right = len(view_counts) - 1
# Iterate n/2 times
while left < right:
# Calculate the average of the smallest and largest view counts
average = (view_counts[left] + view_counts[right]) / 2
# Append the average to the list of average views
average_views.append(average)
# Move the pointers inward
left += 1
right -= 1
# Return the minimum average from the average_views list
return min(average_views)