The Friend Requests I interview question involves two SQL tables: FriendRequest (tracking sent requests) and RequestAccepted (tracking accepted requests). You need to calculate the overall acceptance rate, which is the total number of unique accepted requests divided by the total number of unique sent requests. The result should be rounded to two decimal places. If there are no requests at all, return 0.00.
Meta (Facebook) uses this Database coding problem to test a candidate's ability to calculate business metrics. It evaluates your understanding of finding unique records (using COUNT(DISTINCT ...)) across different tables. It also checks if you can handle edge cases like division by zero, which is a critical skill for building robust data dashboards.
This problem relies on Independent Subqueries and IFNULL.
(sender_id, send_to_id) pairs in the FriendRequest table.(requester_id, accepter_id) pairs in the RequestAccepted table.IFNULL (or COALESCE) to handle the case where the denominator is 0.Sent Requests:
Accepted Requests:
Ratio: .
JOIN the requests and accepts tables. This is unnecessary and often leads to incorrect counts due to duplicates or Cartesian products.COUNT(*) instead of counting unique pairs. A user might click "send" twice, but it's only one logical request.When calculating aggregate ratios across different stages of a funnel (like sent -> accepted), it's almost always safer and faster to calculate the numerator and denominator in separate subqueries rather than trying to join the tables.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Ads Performance | Easy | Solve | |
| Find Books with No Available Copies | Easy | Solve | |
| Number of Comments per Post | Easy | Solve | |
| Project Employees II | Easy | Solve | |
| Reported Posts | Easy | Solve |