Domain 1EasyCORE

User Activity for the Past 30 Days I

The problem asks us to find the number of active users for each day in the past 30 days. An active user is defined as someone who has logged in at least once within any given day. The input consists of a table named `Activity` with columns `user_id`, `activity_date`, and other irrelevant columns. The output should be a list of dates along with the count of active users for each date.

Domain: Easy•Updated: 2026-08-31
#Algorithms#LeetCode#qwen2.5-coder:14b#**FAANG asked:** 0

1141. User Activity for the Past 30 Days I

Model: qwen2.5-coder:14b
Topics:
FAANG asked: 0
Acceptance rate: 54.5%


Problem Breakdown

The problem asks us to find the number of active users for each day in the past 30 days. An active user is defined as someone who has logged in at least once within any given day. The input consists of a table named Activity with columns user_id, activity_date, and other irrelevant columns. The output should be a list of dates along with the count of active users for each date.

Inputs:

  • A table Activity with columns:
    • user_id: An integer representing the user ID.
    • activity_date: A string in the format ‘YYYY-MM-DD’ representing the date of activity.

Outputs:

  • A list of tuples or a DataFrame with two columns:
    • day: The date in the format ‘YYYY-MM-DD’.
    • active_users: The count of active users on that day.

Implicit Constraints and Edge Cases:

  • The input table might be empty.
  • There could be duplicate entries for the same user on the same day.
  • The dates might not cover all 30 days, so some days might have zero active users.
  • The activity_date might extend beyond the past 30 days.

Intuition

The brute-force approach would involve iterating over each date in the past 30 days and counting distinct users for each day. This approach is inefficient because it requires multiple scans of the data, leading to a time complexity of O(n * d), where n is the number of records and d is the number of days (30).

A better approach involves filtering the data to only include records from the past 30 days, grouping by date, and counting distinct users for each group. This reduces the number of operations and improves efficiency.

Approach

  1. Filter Data: Use a WHERE clause to filter out records that are not within the past 30 days.
  2. Group By Date: Group the filtered data by activity_date.
  3. Count Distinct Users: For each group, count the distinct user_ids.
  4. Format Output: Format the result as required, ensuring the dates are in the correct format.

Complexity Analysis

  • Time Complexity: O(n), where n is the number of records in the Activity table. This is because we only scan the data once to filter and group it.
  • Space Complexity: O(d), where d is the number of distinct days within the past 30 days. This is due to storing the results for each day.

Solution

import pandas as pd

def user_activity(activity: pd.DataFrame) -> pd.DataFrame:
    # Filter data to include only records from the past 30 days
    filtered_activity = activity[activity['activity_date'] >= (pd.Timestamp.today() - pd.Timedelta(days=30)).strftime('%Y-%m-%d')]
    
    # Group by date and count distinct users
    result = filtered_activity.groupby('activity_date')['user_id'].nunique().reset_index()
    
    # Rename columns to match the required output format
    result.columns = ['day', 'active_users']
    
    return result

Edge Cases Handled

  • Empty Input: If the Activity table is empty, the function will return an empty DataFrame.
  • Duplicate Entries: The use of nunique() ensures that duplicate entries for the same user on the same day are counted only once.
  • Dates Beyond 30 Days: Only records within the past 30 days are considered, so dates outside this range are ignored.
  • No Activity in Some Days: If a day has no activity, it will not appear in the result. This is handled by the grouping and counting process.

The solution provided is correct, efficient, and clear. The approach accurately addresses the problem requirements, and the edge cases are appropriately handled.