Popularity Percentage - Problem
Social Media Popularity Calculator
You're working at Meta/Facebook and need to calculate each user's popularity percentage on the platform. Given a friendship table where each row represents a mutual friendship between two users, your task is to determine how popular each user is relative to the entire user base.
The popularity percentage is calculated as:
Your solution should:
• Calculate each user's friend count
• Determine the total number of unique users
• Compute popularity percentage rounded to 2 decimal places
• Return results ordered by user ID ascending
Table Schema:
+-------------+------+
| Column Name | Type |
+-------------+------+
| user1 | int |
| user2 | int |
+-------------+------+
Note: (user1, user2) is the primary key. Each friendship is bidirectional.
You're working at Meta/Facebook and need to calculate each user's popularity percentage on the platform. Given a friendship table where each row represents a mutual friendship between two users, your task is to determine how popular each user is relative to the entire user base.
The popularity percentage is calculated as:
(Number of friends a user has / Total number of users on platform) × 100Your solution should:
• Calculate each user's friend count
• Determine the total number of unique users
• Compute popularity percentage rounded to 2 decimal places
• Return results ordered by user ID ascending
Table Schema:
Friends+-------------+------+
| Column Name | Type |
+-------------+------+
| user1 | int |
| user2 | int |
+-------------+------+
Note: (user1, user2) is the primary key. Each friendship is bidirectional.
Input & Output
example_1.sql — Basic Friendship Network
$
Input:
Friends table:
+-------+-------+
| user1 | user2 |
+-------+-------+
| 1 | 2 |
| 2 | 3 |
+-------+-------+
›
Output:
+-------+------------+
| user1 | percentage |
+-------+------------+
| 1 | 33.33 |
| 2 | 66.67 |
| 3 | 33.33 |
+-------+------------+
💡 Note:
Total users: 3 (1,2,3). User 1: 1 friend (2) → 1/3*100 = 33.33%. User 2: 2 friends (1,3) → 2/3*100 = 66.67%. User 3: 1 friend (2) → 1/3*100 = 33.33%.
example_2.sql — Larger Network
$
Input:
Friends table:
+-------+-------+
| user1 | user2 |
+-------+-------+
| 1 | 2 |
| 1 | 3 |
| 2 | 3 |
| 2 | 4 |
+-------+-------+
›
Output:
+-------+------------+
| user1 | percentage |
+-------+------------+
| 1 | 50.00 |
| 2 | 75.00 |
| 3 | 50.00 |
| 4 | 25.00 |
+-------+------------+
💡 Note:
Total users: 4 (1,2,3,4). User 1: friends with 2,3 → 2/4*100 = 50%. User 2: friends with 1,3,4 → 3/4*100 = 75%. User 3: friends with 1,2 → 2/4*100 = 50%. User 4: friends with 2 → 1/4*100 = 25%.
example_3.sql — Single Friendship
$
Input:
Friends table:
+-------+-------+
| user1 | user2 |
+-------+-------+
| 1 | 2 |
+-------+-------+
›
Output:
+-------+------------+
| user1 | percentage |
+-------+------------+
| 1 | 50.00 |
| 2 | 50.00 |
+-------+------------+
💡 Note:
Total users: 2 (1,2). Each user has exactly 1 friend, so both have 1/2*100 = 50% popularity.
Visualization
Tap to expand
Understanding the Visualization
1
Identify All Students
Collect every unique student ID from both sides of friendship pairs
2
Count Each Student's Friends
For each student, count how many friendships they appear in
3
Calculate School Size
Determine the total number of students in the school
4
Compute Popularity Percentage
Divide each student's friend count by total students, multiply by 100
Key Takeaway
🎯 Key Insight: Use CTEs to efficiently calculate friend counts and total users in a single query, avoiding the O(n²) complexity of nested subqueries while maintaining clean, readable SQL code.
Time & Space Complexity
Time Complexity
O(n)
Single pass through the data with efficient joins
✓ Linear Growth
Space Complexity
O(n)
Space for CTEs and result set
⚡ Linearithmic Space
Constraints
- 1 ≤ Friends table rows ≤ 500
- 1 ≤ user1, user2 ≤ 106
- user1 ≠ user2 (no self-friendships)
- Each friendship pair appears only once
- Important: Friendships are bidirectional (if user1 friends user2, then user2 friends user1)
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code