Friend Requests I: Overall Acceptance Rate - Problem

You are given two tables representing a social network's friend request system.

Table: FriendRequest

  • sender_id: ID of the user who sent the request
  • send_to_id: ID of the user who received the request
  • request_date: Date when the request was sent

Table: RequestAccepted

  • requester_id: ID of the user who sent the request
  • accepter_id: ID of the user who received the request
  • accept_date: Date when the request was accepted

Find the overall acceptance rate of friend requests, calculated as the number of acceptances divided by the number of requests. Return the result rounded to 2 decimal places.

Important Notes:

  • Both tables may contain duplicates - count unique request/acceptance pairs only
  • Accepted requests don't need to exist in the FriendRequest table
  • If there are no requests, return 0.00

Table Schema

FriendRequest
Column Name Type Description
sender_id int ID of user sending friend request
send_to_id int ID of user receiving friend request
request_date date Date when request was sent
Primary Key: None (may contain duplicates)
Note: Contains friend request data, may have duplicate entries
RequestAccepted
Column Name Type Description
requester_id int ID of user who sent the accepted request
accepter_id int ID of user who accepted the request
accept_date date Date when request was accepted
Primary Key: None (may contain duplicates)
Note: Contains accepted request data, may have duplicate entries

Input & Output

Example 1 — Basic Acceptance Rate
Input Tables:
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 distinct friend requests and 4 distinct acceptances. The acceptance rate is 4/5 = 0.80 or 80%.

Example 2 — No Requests Edge Case
Input Tables:
FriendRequest
sender_id send_to_id request_date
RequestAccepted
requester_id accepter_id accept_date
1 2 2016-06-03
Output:
accept_rate
0.00
💡 Note:

When there are no friend requests, the acceptance rate should be 0.00 even if there are some acceptances recorded.

Example 3 — Handling Duplicates
Input Tables:
FriendRequest
sender_id send_to_id request_date
1 2 2016-06-01
1 2 2016-06-02
RequestAccepted
requester_id accepter_id accept_date
1 2 2016-06-03
1 2 2016-06-04
Output:
accept_rate
1.00
💡 Note:

Even though there are duplicate entries, we count only 1 distinct request (1→2) and 1 distinct acceptance (1→2), giving a rate of 1/1 = 1.00.

Constraints

  • 1 ≤ sender_id, send_to_id ≤ 1000
  • 1 ≤ requester_id, accepter_id ≤ 1000
  • Tables may contain duplicate records
  • Result should be rounded to 2 decimal places

Visualization

Tap to expand
Friend Requests: Overall Acceptance Rate INPUT FriendRequest Table sender_id send_to_id 1 2 1 3 1 2 (duplicate) RequestAccepted Table requester_id accepter_id 1 2 1 3 Request Flow: 1 2 3 User nodes ALGORITHM STEPS 1 Count Unique Requests SELECT COUNT(DISTINCT sender_id, send_to_id) 2 Count Unique Accepts SELECT COUNT(DISTINCT requester_id, accepter_id) 3 Handle Edge Cases If requests = 0, return NULL (avoid /0) 4 Calculate Rate accepts / requests Round to 2 decimals Calculation: Unique Requests: 2 Unique Accepts: 2 Rate = 2/2 = 1.00 FINAL RESULT Output Table accept_rate 1.00 Acceptance Rate Gauge 100% 0% 100% SQL Query: SELECT ROUND( COUNT(DISTINCT a.requester_id, a.accepter_id) / COUNT(DISTINCT r.sender_id, r.send_to_id), 2 ) AS accept_rate Key Insight: DISTINCT removes duplicate request/acceptance pairs. This ensures each unique friend request is counted only once, regardless of how many times it appears in the tables. IFNULL/COALESCE handles the edge case where no requests exist to avoid division by zero errors. TutorialsPoint - Friend Requests I: Overall Acceptance Rate | Optimal Solution
Asked in
Facebook 25 LinkedIn 18
28.0K Views
Medium Frequency
~12 min Avg. Time
890 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