Codepath

VIP Passes and Guests

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 number of guests who have VIP passes by comparing two strings.
  • Q: What are the inputs?

    • A: Two strings vip_passes and guests, where vip_passes represents the types of guests with VIP passes and guests represents the types of guests at the festival.
  • Q: What are the outputs?

    • A: An integer representing the number of guests who have VIP passes.
  • Q: Are the characters case-sensitive?

    • A: Yes, characters are case-sensitive.

P-lan

Plan the solution with appropriate visualizations and pseudocode.

For this problem, the Plan step was provided to you.

1. Create an empty set called vip_set.
2. For each character in vip_passes, add it to vip_set.
3. Initialize a counter variable to 0.
4. For each character in guests:
   - If the character is in vip_set, increment the count by 1.
5. Return the count.

I-mplement

def num_VIP_guests(vip_passes, guests):
    # Step 1: Create an empty set called vip_set
    vip_set = set()
    
    # Step 2: For each character in vip_passes, add it to vip_set
    for vip in vip_passes:
        vip_set.add(vip)
    
    # Step 3: Initialize a counter variable to 0
    count = 0
    
    # Step 4: For each character in guests, if it is in vip_set, increment the count
    for guest in guests:
        if guest in vip_set:
            count += 1
    
    # Step 5: Return the count
    return count
Fork me on GitHub