Unit 2 Session 1 (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 problem asking for?
Q: What are the inputs?
audiences
of positive integers representing audience sizes for different performances.Q: What are the outputs?
Q: Are there any constraints on the values in the array?
Plan the solution with appropriate visualizations and pseudocode.
Note: Like many interview questions, this problem can be solved multiple ways. If you chose to approach this with a dictionary, check out the Performances-With-Maximum-Audience solution guide.
General Idea: Find the maximum audience size, then sum the audience sizes of all performances that match this maximum size without using a dictionary.
1) Find the maximum value in the `audiences` array and store it in `max_audience`.
2) Initialize a variable `total_max_audience` to 0.
3) Iterate through the `audiences` array.
- For each audience size that equals `max_audience`, add it to `total_max_audience`.
4) Return the value of `total_max_audience`.
⚠️ Common Mistakes
max_audience
is correctly identified.audiences
array is empty by returning 0.def max_audience_performances(audiences):
if not audiences:
return 0
# Step 1: Find the maximum audience size
max_audience = max(audiences)
# Step 2: Sum all performances that have the maximum audience size
total_max_audience = 0
for audience in audiences:
if audience == max_audience:
total_max_audience += audience
return total_max_audience