Find Emotionally Consistent Users Problem
Description
LeetCode Problem 3808.
Table: reactions
+--------------+---------+
| Column Name | Type |
+--------------+---------+
| user_id | int |
| content_id | int |
| reaction | varchar |
+--------------+---------+
(user_id, content_id) is the primary key (unique value) for this table.
Each row represents a reaction given by a user to a piece of content.
Write a solution to identify emotionally consistent users based on the following requirements:
- For each user, count the total number of reactions they have given.
- Only include users who have reacted to at least
5different content items. - A user is considered emotionally consistent if at least
60%of their reactions are of the same type.
Return the result table ordered by reaction_ratio in descending order and then by user_id in ascending order.
Note:
reaction_ratioshould be rounded to2decimal places
The result format is in the following example.
Example:
Input:
reactions table:
+---------+------------+----------+
| user_id | content_id | reaction |
+---------+------------+----------+
| 1 | 101 | like |
| 1 | 102 | like |
| 1 | 103 | like |
| 1 | 104 | wow |
| 1 | 105 | like |
| 2 | 201 | like |
| 2 | 202 | wow |
| 2 | 203 | sad |
| 2 | 204 | like |
| 2 | 205 | wow |
| 3 | 301 | love |
| 3 | 302 | love |
| 3 | 303 | love |
| 3 | 304 | love |
| 3 | 305 | love |
+---------+------------+----------+
Output:
+---------+-------------------+----------------+
| user_id | dominant_reaction | reaction_ratio |
+---------+-------------------+----------------+
| 3 | love | 1.00 |
| 1 | like | 0.80 |
+---------+-------------------+----------------+
Explanation:
- User 1:
- Total reactions = 5
- like appears 4 times
- reaction_ratio = 4 / 5 = 0.80
- Meets the 60% consistency requirement
- User 2:
- Total reactions = 5
- Most frequent reaction appears only 2 times
- reaction_ratio = 2 / 5 = 0.40
- Does not meet the consistency requirement
- User 3:
- Total reactions = 5
- 'love' appears 5 times
- reaction_ratio = 5 / 5 = 1.00
- Meets the consistency requirement
The Results table is ordered by reaction_ratio in descending order, then by user_id in ascending order.
Solution
Approach: Count per reaction, keep each user's most frequent one
Group by (user_id, reaction) to get how often each user gave each reaction. A window SUM(COUNT(*)) OVER (PARTITION BY user_id) adds the user's total number of reactions to every one of their rows, and ROW_NUMBER ordered by count descending marks the most frequent reaction as rank 1.
For that top row the rules are:
- the user has at least 5 reactions (
(user_id, content_id)is the key, so reactions equal different content items), and cnt / total >= 0.6.
Because the threshold is above one half, no two different reactions can both reach it, so the tie-break in the ranking never decides the answer. The ratio is rounded only in the output. The result is ordered by ratio descending, then user id. User 2 reacts five times but their best reaction covers only 2 of 5.
The Pandas version builds the same counts, sorts them and takes the first row per user.
WITH counts AS (
SELECT user_id,
reaction,
COUNT(*) AS cnt,
SUM(COUNT(*)) OVER (PARTITION BY user_id) AS total,
ROW_NUMBER() OVER (PARTITION BY user_id ORDER BY COUNT(*) DESC, reaction) AS rn
FROM reactions
GROUP BY user_id, reaction
)
SELECT user_id,
reaction AS dominant_reaction,
ROUND(cnt / total, 2) AS reaction_ratio
FROM counts
WHERE rn = 1
AND total >= 5
AND cnt / total >= 0.6
ORDER BY reaction_ratio DESC, user_id;