Activity Participants - Problem
You're working on a social platform that tracks friend activities! You have two tables: one containing Friends with their favorite activities, and another containing all available Activities.
Your task is to find activities that are neither too popular nor too unpopular - essentially the "middle ground" activities that don't have the maximum or minimum number of participants.
Goal: Return the names of activities that have neither the highest nor the lowest participation count.
Tables:
Friends: Contains friend ID, name, and their chosen activityActivities: Contains all available activity names
Key Points:
- Each friend participates in exactly one activity
- Some activities might have zero participants
- We want to exclude both the most and least popular activities
Input & Output
example_1.sql โ Basic Case
$
Input:
Friends: [(1,'Alice','hiking'), (2,'Bob','swimming'), (3,'Charlie','hiking'), (4,'David','cycling'), (5,'Eve','swimming')]
Activities: ['hiking','swimming','cycling','reading']
โบ
Output:
['cycling']
๐ก Note:
hiking has 2 participants (max), reading has 0 participants (min), swimming has 2 participants (max). Only cycling with 1 participant is neither min nor max.
example_2.sql โ Multiple Middle Activities
$
Input:
Friends: [(1,'Alice','chess'), (2,'Bob','tennis'), (3,'Charlie','chess'), (4,'David','soccer')]
Activities: ['chess','tennis','soccer','reading','painting']
โบ
Output:
['tennis','soccer']
๐ก Note:
chess has 2 participants (max), reading and painting have 0 participants (min). Tennis and soccer each have 1 participant, which is the middle count.
example_3.sql โ Edge Case
$
Input:
Friends: [(1,'Alice','dancing')]
Activities: ['dancing','singing','acting']
โบ
Output:
[]
๐ก Note:
dancing has 1 participant (max), singing and acting have 0 participants (min). No activities have counts that are neither min nor max.
Visualization
Tap to expand
Understanding the Visualization
1
Gather Data
Collect all employees' activity preferences and available activities
2
Count Participants
Tally how many people signed up for each activity
3
Find Extremes
Identify the most and least popular activities
4
Select Moderate
Choose activities with moderate participation for balanced planning
Key Takeaway
๐ฏ Key Insight: Use window functions to calculate participation statistics and filter activities with moderate engagement levels in a single efficient query.
Time & Space Complexity
Time Complexity
O(n log n)
Single pass with sorting for window functions, optimized by database engine
โก Linearithmic
Space Complexity
O(n)
Space for intermediate results and window function calculations
โก Linearithmic Space
Constraints
- 1 โค Friends table rows โค 104
- 1 โค Activities table rows โค 50
- All activity names in Friends table exist in Activities table
- Activity names are case-sensitive
- Each friend participates in exactly one activity
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code