Codepath

Performances With Maximum Audience II

Unit 2 Session 1 (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 problem asking for?

    • A: The problem asks to return the combined audience size of all performances that have the maximum audience size from an array of audience sizes.
  • Q: What are the inputs?

    • A: An array audiences of positive integers representing audience sizes for different performances.
  • Q: What are the outputs?

    • A: An integer representing the combined audience size of all performances with the maximum audience size.
  • Q: Are there any constraints on the values in the array?

    • A: The values should be positive integers.

P-lan

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

  • Ensure that the max_audience is correctly identified.
  • Handle cases where the audiences array is empty by returning 0.

I-mplement

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
Fork me on GitHub