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
Longest Facebook Activity Streak
00:00
5 left

Longest Facebook Activity Streak

MediumSQL · PostgreSQL

Problem

Facebook wants to measure habit strength by finding each user's longest streak of consecutive daily activity. Write a SQL query to return the longest daily activity streak per user.

Requirements

  1. Treat multiple events by the same user on the same calendar date as a single active day.
  2. A streak is a sequence of activity dates where each date is exactly 1 day after the previous active date.
  3. Return one row per user with their user_id, longest streak length, streak start date, and streak end date.
  4. If a user has multiple streaks with the same maximum length, return the earliest streak.
  5. Order the final output by user_id ascending.

Schema

facebook_activity
ColumnTypeDescription
activity_idPKINTUnique identifier for each activity event
user_idINTFacebook user identifier
activity_dateTIMESTAMPTimestamp when the activity occurred
surfaceVARCHAR(50)Meta surface where the event occurred
activity_typeVARCHAR(50)Type of activity performed
device_typeVARCHAR(20)Device used for the activity
Tablesfacebook_activity
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.