Codepath

Souvenir Declutter

U-nderstand

Understand what the interviewer is asking for by using test cases and questions about the problem.

  • Q
    • What is the desired outcome?
    • To filter out souvenirs whose occurrences are greater than or equal to the threshold.
    • What input is provided?
    • A list of strings souvenirs and an integer threshold.

P-lan

Plan the solution with appropriate visualizations and pseudocode.

General Idea: Count the frequency of each souvenir and filter those below the threshold.

1) Create a dictionary `souvenir_count` to count the frequency of each souvenir.
2) Iterate over the `souvenirs` list to populate the dictionary.
3) Use a list comprehension to filter and return only those souvenirs with a count less than the threshold.

⚠️ Common Mistakes

  • Counting souvenirs incorrectly by not checking their presence in the dictionary first.

I-mplement

def declutter(souvenirs, threshold):
    # Count the frequency of each souvenir
    souvenir_count = {}
    for souvenir in souvenirs:
        if souvenir in souvenir_count:
            souvenir_count[souvenir] += 1
        else:
            souvenir_count[souvenir] = 1
    
    # Filter souvenirs based on the threshold
    decluttered_souvenirs = []
    for souvenir in souvenirs:
        if souvenir_count[souvenir] < threshold:
            decluttered_souvenirs.append[souvenir]
    
    return decluttered_souvenirs
Fork me on GitHub