BLOGLLM Logo
Go Pro
Guest
Not signed in
All BlogsCoursesPracticeInterview prepSeriesTagsBookmarksPricing
Log in
All BlogsCoursesPracticeTagsBookmarksPricing
My PrepProblemsStudy PlansResearch PapersRoadmapInterviewContestsLeaderboardGames
Articles
Practice / SQL19 of 22 in topic

635. Compute day-1 and day-7 retention per signup cohort

HardSQL Pro
Asked atMicrosoft

Why it matters D1 and D7 retention are the first numbers investors and product teams ask for in any consumer AI app.

Retention says if people come back. Day-1 retention is the share of a cohort active the day after signup, day-7 is the share active exactly a week later. Activity logs have many rows per user per day.

Write query() returning a SQLite SQL string. Tables: users(user_id INTEGER, signup_day TEXT) and activity(user_id INTEGER, day TEXT), dates as 'YYYY-MM-DD'. activity can have many rows per user per day, and some users have no activity at all.

Return one row per signup_day: (signup_day, cohort_size, d1_users, d7_users) where cohort_size counts users who signed up that day, d1_users counts those with activity on signup_day + 1 day, and d7_users those with activity on signup_day + 7 days. Order by signup_day ascending.

Functiondef query()

Example 1

users 1 and 2 sign up on 2026-01-01; user 1 is active twice on 2026-01-02 and once on 2026-01-08; user 2 never comes back. Result: [('2026-01-01', 2, 1, 1)].

Constraints

  • Python 3 with sqlite3. Code runs in your browser.
  • Run checks the sample tests. Submit checks the full hidden test set (Pro).
Hint 1

LEFT JOIN activity so inactive users stay in the cohort, and count COUNT(DISTINCT CASE WHEN a.day = date(u.signup_day, '+1 day') THEN u.user_id END).

Hint 2

Start from users so inactive users still count in the cohort.

Hint 3

SQLite's date(d, '+1 day') does calendar math on 'YYYY-MM-DD' strings.

Hint 4

Count distinct user ids inside a CASE for each target day.

Common mistakes
  • Inner joining activity and losing users who never came back.
  • Counting activity rows, not users.
  • Using day >= signup + 1, which is 'ever returned', not day-1 retention.
Understand the idea

Retention measures how many people from a group come back after a fixed time. A cohort is everyone who signed up on the same day. Day-N retention is the share of the cohort that was active exactly N days after signup.

Worked example

users 1 and 2 sign up on Jan 1. User 1 is active on Jan 2 (twice) and Jan 8. Day 1 is Jan 2, so d1_users = 1 (user 1 once, not twice). Day 7 is Jan 8, so d7_users = 1. Cohort size is 2, so both rates are 50 percent.

dN_users = COUNT(DISTINCT CASE WHEN activity_day = signup_day + N days THEN user_id END)

Use a LEFT JOIN from users so inactive users stay in the denominator.

Chat apps, coding assistants and every subscription product track D1, D7 and D30 retention by cohort.

Topics
SQL
Companies1
  • Microsoft the topic appears in interview reports Microsoft Interview Prep: Questions, Process & Study Plan

From public interview reports and guides. Not affiliated with or endorsed by these companies.

Similar questions
  • Compute day-over-day percent change in API cost per team with LAGHard
  • Split agent events into sessions by idle gap and report sessions per userHard
  • Build a three-step signup funnel where steps must happen in orderHard
  • Compute the median latency per model with window functionsHard
All SQL problems
Report
635 / 903
Python 3solution.pyPython idle
1
2
3
4
def query():
# your code here
pass
​
query()

This case uses generated data, so it runs as written. Press + to add your own case.

Run checks these cases in your browser, no sign-in needed. Submit checks the full hidden test set with Pro.

Ctrl/Cmd + Enter to run
© BlogLLM, Bengaluru, IndiaAboutTermsPrivacyRefundsDMCAContactRSS