Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
ExperiencesCoaching
New
Practice
Mock InterviewsTimed interview simulations with feedbackSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Get started
Friend Pairs Within Seven Days
00:00
5 left

Friend Pairs Within Seven Days

MediumSQL · PostgreSQL

Problem

At Meta, the Facebook Friends graph team wants to identify users whose first friendship creation happened within 7 days of each other. This can be useful when analyzing early-network formation patterns around surfaces like Facebook Groups and downstream AARRR activation behavior.

Write a SQL query to find pairs of users who became friends within 7 days of each other.

Requirements

  1. Use each user's earliest friendship timestamp as the date they first became friends on Facebook.
  2. Return each qualifying pair only once, with user_id_1 < user_id_2.
  3. Include the two users' first friendship timestamps and the absolute day difference.
  4. Exclude users who have no valid friendship timestamp.
  5. Order the output by days_apart, then user_id_1, then user_id_2.

Schema

friendships
ColumnTypeDescription
friendship_idPKINTUnique friendship event identifier
user_id_1INTFirst user in the friendship edge
user_id_2INTSecond user in the friendship edge
became_friends_atTIMESTAMPTimestamp when the friendship was created
surfaceVARCHAR(50)Meta product surface where the friendship was initiated
Tablesfriendships
Interviewer

Whenever you're ready, walk me through your approach or go straight to the code. The requirements and the one table are in the Question tab.

You need to log in / sign up to run or submit.
CodePostgreSQL
Sign up free to run your codeLog inLn 1
Run your query to see results here.