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
๐Ÿค Friend Request Analytics Dashboard๐Ÿ“ค Friend RequestsABCDDISTINCT: 2 unique pairsโœ… Accepted RequestsABโœ“ AcceptedDISTINCT: 1 unique pair๐Ÿ“Š Success Rate1 รท 2 = 50.00%ROUND(0.5, 2) = 0.50๐Ÿ”„ Edge Case HandlingIF no requests THEN 0.00 ELSE calculated_rate
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

n
2n
โœ“ Linear Growth
Space Complexity
O(k)

Where k is the number of unique pairs, used by database for DISTINCT processing

n
2n
โœ“ 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
Asked in
Facebook 25 LinkedIn 18 Instagram 12 Twitter 8
78.0K Views
Medium Frequency
~15 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen