Friend Requests II: Who Has the Most Friends - Problem
๐ค Friend Requests II: Who Has the Most Friends
You're working on a social media platform's analytics team! Given a database table RequestAccepted that tracks all accepted friend requests, you need to find the person with the most friends.
๐ Table Structure:
| Column Name | Type | Description |
|---|---|---|
requester_id | int | ID of user who sent the request |
accepter_id | int | ID of user who accepted the request |
accept_date | date | When the request was accepted |
Key Points:
- Each row represents a bidirectional friendship (if A sends to B and B accepts, both A and B are friends)
- The combination
(requester_id, accepter_id)is unique - You need to return both the person ID and their total friend count
- There's guaranteed to be exactly one person with the maximum friends
Goal: Write a SQL query to find who has the most friends and how many friends they have.
Input & Output
example_1.sql โ Basic Friend Network
$
Input:
RequestAccepted table:
| requester_id | accepter_id | accept_date |
|--------------|-------------|-------------|
| 1 | 2 | 2016-06-03 |
| 1 | 3 | 2016-06-08 |
| 2 | 3 | 2016-06-08 |
| 3 | 4 | 2016-06-09 |
โบ
Output:
| id | num |
|----|-----|
| 3 | 3 |
๐ก Note:
User 3 has the most friends. They are connected to users 1, 2, and 4. User 3 appears in 3 friendship records: as accepter with users 1 and 2, and as requester with user 4.
example_2.sql โ Single Friendship
$
Input:
RequestAccepted table:
| requester_id | accepter_id | accept_date |
|--------------|-------------|-------------|
| 1 | 2 | 2016-06-03 |
โบ
Output:
| id | num |
|----|-----|
| 1 | 1 |
| 2 | 1 |
๐ก Note:
Both users have exactly 1 friend each. Either could be returned as they're tied, but the problem guarantees only one person has the maximum.
example_3.sql โ Linear Chain
$
Input:
RequestAccepted table:
| requester_id | accepter_id | accept_date |
|--------------|-------------|-------------|
| 1 | 2 | 2016-06-03 |
| 2 | 3 | 2016-06-04 |
| 3 | 4 | 2016-06-05 |
| 4 | 5 | 2016-06-06 |
โบ
Output:
| id | num |
|----|-----|
| 2 | 2 |
| 3 | 2 |
| 4 | 2 |
๐ก Note:
In a linear chain, users 2, 3, and 4 each have 2 friends (their neighbors), while users 1 and 5 have only 1 friend each.
Visualization
Tap to expand
Understanding the Visualization
1
Collect All Relationships
Each accepted request creates a bidirectional friendship - both requester and accepter gain a friend
2
Count Per Person
Use UNION ALL to list each person every time they appear in a friendship, then group and count
3
Find the Winner
Sort by friend count descending and pick the top person
Key Takeaway
๐ฏ Key Insight: Friendships are bidirectional, so we count each person's appearances in both requester and accepter roles using UNION ALL, then group and count efficiently.
Time & Space Complexity
Time Complexity
O(n log n)
UNION ALL is O(n), GROUP BY with sorting is O(n log n) due to internal sorting for aggregation
โก Linearithmic
Space Complexity
O(n)
Temporary space needed for the unified result set before grouping
โก Linearithmic Space
Constraints
- 1 โค number of accepted requests โค 500
- 1 โค requester_id, accepter_id โค 500
- requester_id โ accepter_id (no self-friend requests)
- All dates are valid
- Guaranteed exactly one person has the maximum number of friends
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code