Find Users with Persistent Behavior Patterns Problem
Description
LeetCode Problem 3832.
Table: activity
+--------------+---------+
| Column Name | Type |
+--------------+---------+
| user_id | int |
| action_date | date |
| action | varchar |
+--------------+---------+
(user_id, action_date, action) is the primary key (unique value) for this table.
Each row represents a user performing a specific action on a given date.
Write a solution to identify behaviorally stable users based on the following definition:
- A user is considered behaviorally stable if there exists a sequence of at least
5consecutive days such that:- The user performed exactly one action per day during that period.
- The action is the same on all those consecutive days.
- If a user has multiple qualifying sequences, only consider the sequence with the maximum length.
- If multiple qualifying sequences have the same maximum length, consider the sequence with the lexicographically smallest action. If there is still a tie, consider the sequence with the latest start date.
Return the result table ordered by streak_length in descending order, then by user_id in ascending order.
The result format is in the following example.
Example:
Input:
activity table:
+---------+-------------+--------+
| user_id | action_date | action |
+---------+-------------+--------+
| 1 | 2024-01-01 | login |
| 1 | 2024-01-02 | login |
| 1 | 2024-01-03 | login |
| 1 | 2024-01-04 | login |
| 1 | 2024-01-05 | login |
| 1 | 2024-01-06 | logout |
| 2 | 2024-01-01 | click |
| 2 | 2024-01-02 | click |
| 2 | 2024-01-03 | click |
| 2 | 2024-01-04 | click |
| 3 | 2024-01-01 | view |
| 3 | 2024-01-02 | view |
| 3 | 2024-01-03 | view |
| 3 | 2024-01-04 | view |
| 3 | 2024-01-05 | view |
| 3 | 2024-01-06 | view |
| 3 | 2024-01-07 | view |
+---------+-------------+--------+
Output:
+---------+--------+---------------+------------+------------+
| user_id | action | streak_length | start_date | end_date |
+---------+--------+---------------+------------+------------+
| 3 | view | 7 | 2024-01-01 | 2024-01-07 |
| 1 | login | 5 | 2024-01-01 | 2024-01-05 |
+---------+--------+---------------+------------+------------+
Explanation:
- User 1:
- Performed login from 2024-01-01 to 2024-01-05 on consecutive days
- Each day has exactly one action, and the action is the same
- Streak length = 5 (meets minimum requirement)
- The action changes on 2024-01-06, ending the streak
- User 2:
- Performed click for only 4 consecutive days
- Does not meet the minimum streak length of 5
- Excluded from the result
- User 3:
- Performed view for 7 consecutive days
- This is the longest valid sequence for this user
- Included in the result
The Results table is ordered by streak_length in descending order, then by user_id in ascending order
Solution
Approach: Gaps and islands on the single-action days
A qualifying day is a day on which the user did exactly one action, so the first step is to keep only those days (GROUP BY user_id, action_date HAVING COUNT(*) = 1). A day with two actions breaks any streak through it.
Then find runs of consecutive dates with the same action using the classic "date minus row number" trick. Number each user's days per action in date order and subtract that number of days from the date: for consecutive dates the result is constant, and any missing day shifts it. Rows that share (user_id, action, grp) form one streak, giving its length, start and end.
- Keep streaks of at least 5 days.
- If a user has several, pick one with a window ordered by length descending, then action ascending, then start date descending, which are exactly the tie-break rules of the statement.
- Order the final rows by streak length descending, then user id.
User 2 stops at four days, and user 1's login run is cut off by the logout on 6 January but still reaches five days.
The Pandas version uses cumcount for the row number and subtracts it as a timedelta.
WITH single_action_days AS (
SELECT user_id, action_date, MIN(action) AS action
FROM activity
GROUP BY user_id, action_date
HAVING COUNT(*) = 1
),
marked AS (
SELECT user_id,
action,
action_date,
DATE_SUB(
action_date,
INTERVAL ROW_NUMBER() OVER (PARTITION BY user_id, action ORDER BY action_date) DAY
) AS grp
FROM single_action_days
),
streaks AS (
SELECT user_id,
action,
COUNT(*) AS streak_length,
MIN(action_date) AS start_date,
MAX(action_date) AS end_date
FROM marked
GROUP BY user_id, action, grp
HAVING COUNT(*) >= 5
),
best AS (
SELECT *,
ROW_NUMBER() OVER (
PARTITION BY user_id ORDER BY streak_length DESC, action, start_date DESC
) AS rn
FROM streaks
)
SELECT user_id, action, streak_length, start_date, end_date
FROM best
WHERE rn = 1
ORDER BY streak_length DESC, user_id;