Ask Claude about this

User Activity Analysis

Business Context

At Meta/Facebook, understanding user engagement patterns is crucial for platform growth and retention. This analysis identifies users who demonstrate high engagement by being active on consecutive days. By tracking users who consistently interact with our platform through posts or comments on consecutive days, we can better understand our most engaged user segments, optimize notification strategies, and measure the effectiveness of our product features in driving daily active usage.

Technical Requirements

The primary objective is to develop a PostgreSQL query for the following:

Task: Find users who were active (made a post or comment) on consecutive days.

Definition of "Active":

  • A user is considered active on a day if they made at least one post OR left at least one comment on that day.
  • "Consecutive days" means the user was active on at least two days in a row (e.g., active on January 5th AND active on January 6th).
  • Multiple activities by the same user on the same day count as a single day of activity for that user.
  • The output should list the user_id and username of such users. Each user should appear only once in the output, even if they had multiple streaks of consecutive activity.

Database Schema

The database consists of two tables: Users and Activities.

-- Table: Users
CREATE TABLE Users (
    user_id INT PRIMARY KEY,
    username VARCHAR(255) NOT NULL,
    join_date DATE
);

-- Table: Activities
CREATE TABLE Activities (
    activity_id INT PRIMARY KEY,
    user_id INT,
    activity_type VARCHAR(50), -- e.g., 'post', 'comment'
    activity_date DATE NOT NULL,
    FOREIGN KEY (user_id) REFERENCES Users(user_id)
);

Sample Data

Assume the following sample data for the Users and Activities tables (join_date and activity_date are DATE type):

Table: Users

user_id username join_date
1 john_doe 2023-01-01
2 jane_smith 2023-01-02
3 bob_johnson 2023-01-03
4 sara_wilson 2023-01-04

Table: Activities

activity_id user_id activity_type activity_date
101 1 post 2023-01-05
102 1 comment 2023-01-06
103 2 post 2023-01-05
104 2 post 2023-01-07
105 3 comment 2023-01-05
106 3 comment 2023-01-06
107 4 post 2023-01-05
108 4 post 2023-01-06
109 4 comment 2023-01-07

Expected Output (based on sample data):

user_id username
1 john_doe
3 bob_johnson
4 sara_wilson

User 2 (jane_smith) was active on Jan 5 and Jan 7, but not on Jan 6, so not consecutive. User 4 (sara_wilson) was active on Jan 5-6 AND Jan 6-7.

Consecutive Day User Activity

ADVANCED

Write a PostgreSQL query to identify users who were active (made a post or comment) on at least two consecutive days. Display the user_id and username of these users. Each user should appear only once in the output.

Your Turn! What are your thoughts on this problem or alternative ways to solve it? Share your own SQL query attempts or insights in the comments below!

Nerchuko Academy · Free DS Interview Prep