Skip to main content
Back to problems
#1355
Medium Database

Activity participants

Database
72.0% acceptance
Mar 31, 2026
152
48

No description available.

Solution

Pandas
Time O(n)
Space O(1)
LeetCode
solution.pandas
# Table: Friends
# 
# +---------------+---------+
# | Column Name   | Type    |
# +---------------+---------+
# | id            | int     |
# | name          | varchar |
# | activity      | varchar |
# +---------------+---------+
# id is the id of the friend and the primary key for this table in SQL.
# name is the name of the friend.
# activity is the name of the activity which the friend takes part in.
# 
#  
# 
# Table: Activities
# 
# +---------------+---------+
# | Column Name   | Type    |
# +---------------+---------+
# | id            | int     |
# | name          | varchar |
# +---------------+---------+
# In SQL, id is the primary key for this table.
# name is the name of the activity.
# 
#  
# 
# Find the names of all the activities with neither the maximum nor the minimum number of participants.
# 
# Each activity in the Activities table is performed by any person in the table Friends.
# 
# Return the result table in any order.
# 
# The result format is in the following example.
#
# Example 1:
# Input:
# Friends table:
# +------+--------------+---------------+
# | id   | name         | activity      |
# +------+--------------+---------------+
# | 1    | Jonathan D.  | Eating        |
# | 2    | Jade W.      | Singing       |
# | 3    | Victor J.    | Singing       |
# | 4    | Elvis Q.     | Eating        |
# | 5    | Daniel A.    | Eating        |
# | 6    | Bob B.       | Horse Riding  |
# +------+--------------+---------------+
# Activities table:
# +------+--------------+
# | id   | name         |
# +------+--------------+
# | 1    | Eating       |
# | 2    | Singing      |
# | 3    | Horse Riding |
# +------+--------------+
# Output:
# +--------------+
# | activity     |
# +--------------+
# | Singing      |
# +--------------+
# Explanation:
# Eating activity is performed by 3 friends, maximum number of participants, (Jonathan D. , Elvis Q. and Daniel A.)
# Horse Riding activity is performed by 1 friend, minimum number of participants, (Bob B.)
# Singing is performed by 2 friends (Victor J. and Jade W.)

import pandas as pd

def activity_participants(friends: pd.DataFrame, activities: pd.DataFrame) -> pd.DataFrame:
  counts = friends.groupby('activity').size().reset_index(name='cnt')
  if counts.empty:
    return pd.DataFrame({'activity': []})
  max_cnt = counts['cnt'].max()
  min_cnt = counts['cnt'].min()
  result = counts[(counts['cnt'] != max_cnt) & (counts['cnt'] != min_cnt)]
  return result[['activity']]