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 activity
  • Activities: 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
๐ŸŽฏ Activity Participation AnalysisHiking๐Ÿ‘ฅ๐Ÿ‘ฅ2 peopleโŒ MAXCycling๐Ÿ‘ฅ1 personโœ… GOODSwimming๐Ÿ‘ฅ๐Ÿ‘ฅ2 peopleโŒ MAXReadingโˆ…0 peopleโŒ MIN๐ŸŽฏ Optimal Resource AllocationFocus on moderately popular activitiesAvoid overcrowded (max) and ignored (min) activitiesSQL Solution: Window FunctionsCOUNT() + MIN()/MAX() OVER() + Filter
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

n
2n
โšก Linearithmic
Space Complexity
O(n)

Space for intermediate results and window function calculations

n
2n
โšก 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
Asked in
Meta 35 Google 28 Amazon 22 Microsoft 18
23.4K Views
Medium Frequency
~15 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen