Domain 1EasyCORE

Friend Requests I: Overall Acceptance Rate

The problem asks us to calculate the overall acceptance rate of friend requests on a social network. The inputs are two tables: `FriendRequest` and `AcceptRequest`. The `FriendRequest` table contains records of all friend requests sent, while the `AcceptRequest` table contains records of accepted friend requests. We need to compute the ratio of accepted requests to total requests.

Domain: Easy•Updated: 2026-08-31
#Algorithms#LeetCode#qwen2.5-coder:14b#**FAANG asked:** 1

597. Friend Requests I: Overall Acceptance Rate

Model: qwen2.5-coder:14b
Topics:
FAANG asked: 1
Acceptance rate: 41.9%


Problem Breakdown

The problem asks us to calculate the overall acceptance rate of friend requests on a social network. The inputs are two tables: FriendRequest and AcceptRequest. The FriendRequest table contains records of all friend requests sent, while the AcceptRequest table contains records of accepted friend requests. We need to compute the ratio of accepted requests to total requests.

Inputs:

  • FriendRequest: A table with columns sender_id and receiver_id.
  • AcceptRequest: A table with columns requester_id and accepter_id.

Outputs:

  • The overall acceptance rate as a decimal number. If there are no friend requests, the output should be 0.

Implicit Constraints/Edge Cases:

  • Duplicate entries in either table.
  • Empty tables (no friend requests).
  • Negative numbers or zero values in IDs (though typically not expected in such contexts).

Intuition

A brute-force approach would involve counting all unique friend requests and accepted requests, then calculating the ratio. However, this could be inefficient if the tables are large due to potential for duplicate entries.

The key insight is to use SQL’s COUNT function with DISTINCT to ensure we count each request only once, regardless of duplicates. This approach leverages SQL’s set operations to efficiently handle the problem.

Approach

  1. Count Unique Friend Requests: Use a subquery to count distinct pairs of sender_id and receiver_id in the FriendRequest table.
  2. Count Unique Accepted Requests: Similarly, use another subquery to count distinct pairs of requester_id and accepter_id in the AcceptRequest table.
  3. Calculate Acceptance Rate: Divide the number of accepted requests by the total number of requests. Use CAST to ensure the division results in a decimal rather than an integer.
  4. Handle Edge Cases: If there are no friend requests, return 0.

Complexity Analysis

  • Time Complexity: O(n + m), where n is the number of rows in FriendRequest and m is the number of rows in AcceptRequest. The complexity arises from counting distinct pairs in each table.
  • Space Complexity: O(1) for storing intermediate results, as SQL operations are typically in-place.

Solution

SELECT 
    CASE 
        WHEN (SELECT COUNT(DISTINCT sender_id, receiver_id) FROM FriendRequest) = 0 THEN 0.0
        ELSE CAST((SELECT COUNT(DISTINCT requester_id, accepter_id) FROM AcceptRequest) AS FLOAT) / 
             (SELECT COUNT(DISTINCT sender_id, receiver_id) FROM FriendRequest)
    END AS acceptance_rate;

Edge Cases Handled

  • Empty FriendRequest Table: The query returns 0.0 if there are no friend requests.
  • Duplicate Entries: Using DISTINCT ensures that each request is counted only once, regardless of duplicates.
  • No Accepted Requests: If there are no accepted requests, the division results in 0.0 due to the initial CASE check.

The solution provided is correct and efficient. It handles all edge cases as described, and the approach and complexity analysis are accurate. The code is clear and follows the problem requirements effectively.