Friend Requests Sent to People Who Are Already Friends
Last updated: November 29, 2026
Quick Overview
Facebook stores friendships once per pair in either column order, and friend requests as directed events. Find the share of yesterday's friend requests that were sent to someone the sender was already friends with, a sign of a client bug.
Meta
November 29, 20260
0
0 solved
Facebook stores friendships once per pair in either column order, and friend requests as directed events. Find the share of yesterday's friend requests that were sent to someone the sender was already friends with, a sign of a client bug.
Meta's friendship graph is usually stored as undirected edges with an arbitrary column order, which makes naive joins miss half the matches. The interviewer checks whether you normalize pairs before joining and keep the request grain intact.
What the Interviewer Expects
- Normalizes both tables to (smaller_id, larger_id) with LEAST and GREATEST
- Joins on the normalized pair, not on one direction
- Requires the friendship to exist before the request time
- Dedupes the friendship table in case both directions are stored
- Computes the share with the right denominator
Key Topics to Cover
How to Approach This
- Clarify the schema and expected output format before writing queries.
- Use CTEs (WITH clauses) to break complex queries into readable steps.
- Consider window functions (ROW_NUMBER, RANK, LAG, LEAD) for ranking and sequential analysis.
- Watch for NULLs, duplicates, and edge cases in JOINs and GROUP BY.
- For pandas, prefer vectorized operations over row-by-row iteration.
Possible Follow-up Questions
- How would you check whether the friendship table actually stores both directions?
- What if friendships can end, with an ended_at column?
- How would you break the bug rate down by app version?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice SQL ProblemsSample Answer
Clarify the Data
friendships(user_a, user_b, created_at): one row per pair, but which id lands in user_a is arbitrary, and some legacy rows exist in both orders. `friend_requests(request_id, sender_id, recipient_i...
Approach
Normalize every pair into (lo, hi) using LEAST and GREATEST, dedupe the friendship side to one row per pair with the earliest created_at, then LEFT JOIN requests onto it. Use a flag rather tha...