Friend Requests I: Overall Acceptance Rate - Problem
๐ค Friend Request Acceptance Rate Calculator
Imagine you're building a social media platform's analytics dashboard! You need to calculate the overall acceptance rate of friend requests across your platform.
You have two database tables:
๐ค FriendRequest Table:
+----------------+---------+ | Column Name | Type | +----------------+---------+ | sender_id | int | | send_to_id | int | | request_date | date | +----------------+---------+Contains all friend requests sent on the platform.
โ
RequestAccepted Table:
+----------------+---------+ | Column Name | Type | +----------------+---------+ | requester_id | int | | accepter_id | int | | accept_date | date | +----------------+---------+Contains all accepted friend requests.
Your Mission: Calculate the acceptance rate as: (Total Unique Acceptances) / (Total Unique Requests)
โ ๏ธ Important Notes:
- Duplicate requests between same users count as one request
- Multiple acceptances between same users count as one acceptance
- Acceptances might exist without corresponding requests in FriendRequest table
- If no requests exist, return
0.00 - Round result to 2 decimal places
Input & Output
example_1.sql โ Basic Case
$
Input:
FriendRequest:
| sender_id | send_to_id | request_date |
|-----------|------------|--------------|
| 1 | 2 | 2016-06-01 |
| 1 | 3 | 2016-06-01 |
| 1 | 4 | 2016-06-01 |
| 2 | 3 | 2016-06-02 |
| 3 | 4 | 2016-06-09 |
RequestAccepted:
| 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:
| accept_rate |
|-------------|
| 0.80 |
๐ก Note:
There are 5 unique requests: (1,2), (1,3), (1,4), (2,3), (3,4). There are 4 unique acceptances: (1,2), (1,3), (2,3), (3,4). Acceptance rate = 4/5 = 0.80
example_2.sql โ With Duplicates
$
Input:
FriendRequest:
| sender_id | send_to_id | request_date |
|-----------|------------|--------------|
| 1 | 2 | 2016-06-01 |
| 1 | 2 | 2016-06-02 |
| 2 | 1 | 2016-06-03 |
RequestAccepted:
| requester_id | accepter_id | accept_date |
|--------------|-------------|--------------|
| 1 | 2 | 2016-06-03 |
| 1 | 2 | 2016-06-04 |
โบ
Output:
| accept_rate |
|-------------|
| 0.50 |
๐ก Note:
Unique requests: (1,2) and (2,1) = 2 requests. Unique acceptances: only (1,2) = 1 acceptance. Rate = 1/2 = 0.50. Note: (1,2) appearing twice in requests counts as 1, and (1,2) appearing twice in acceptances counts as 1.
example_3.sql โ No Requests Edge Case
$
Input:
FriendRequest:
| sender_id | send_to_id | request_date |
|-----------|------------|--------------|
(empty table)
RequestAccepted:
| requester_id | accepter_id | accept_date |
|--------------|-------------|--------------|
| 1 | 2 | 2016-06-01 |
โบ
Output:
| accept_rate |
|-------------|
| 0.00 |
๐ก Note:
No requests exist, so acceptance rate is 0.00 by definition, even though there are acceptances (which might be from deleted requests or other sources).
Visualization
Tap to expand
Understanding the Visualization
1
Collect Unique Invitations
Scan all friend requests and identify unique sender-receiver pairs, ignoring duplicate invites
2
Collect Unique Acceptances
Scan all acceptances and identify unique requester-accepter pairs, ignoring duplicate accepts
3
Calculate Success Rate
Divide unique acceptances by unique requests, handling the edge case of zero requests
Key Takeaway
๐ฏ Key Insight: Use SQL DISTINCT to automatically handle duplicates and CASE statements for robust edge case handling in a single efficient query.
Time & Space Complexity
Time Complexity
O(n + m)
Where n and m are the number of rows in each table, with database optimizations for DISTINCT operations
โ Linear Growth
Space Complexity
O(k)
Where k is the number of unique pairs, used by database for DISTINCT processing
โ Linear Space
Constraints
- 1 โค sender_id, send_to_id, requester_id, accepter_id โค 1000
- Tables may contain duplicate records (no primary key)
- Tables may be empty
- Dates are in valid format YYYY-MM-DD
- Result must be rounded to 2 decimal places
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code