Skip to content

47. Three-Day Login Streak

Difficulty: Hard · Topics: Window Functions, Aggregations, Dates, Filtering & Selection

logins records each day a user logged in (user_id, login_date as YYYY-MM-DD). A user may log in several times on the same day.

Return the user_id of every user who logged in on at least 3 consecutive days at some point. Each user should appear once.

Row order does not matter; column names must match. Your code is graded on 3 test cases, including hidden edge cases.

Sample data

logins

user_idlogin_date
12024-01-01
12024-01-02
12024-01-03
12024-01-05
22024-01-01
22024-01-03
22024-01-05
32024-01-10
32024-01-10
32024-01-11
32024-01-12
32024-01-12

Expected output

user_id
1
3

Hints

Hint 1De-duplicate (user_id, login_date) first, so repeat logins on a day don't distort the numbering.
Hint 2Number each user's dates with row_number() ordered by login_date.
Hint 3F.date_sub(login_date, rn) yields the same value for every date in an unbroken run — that value identifies the streak.
Hint 4Group by (user_id, grp), count the days, keep counts >= 3.

Learn the concepts

PySpark functions you'll practise

Related problems

Browse

Topics: Window Functions · Joins · Aggregations · Pivot, Unpivot & Rollup · Arrays · Null Handling · Conditional Logic · Dates · Filtering & Selection · Strings · Data Lake · Lakehouse · Streaming · Data Prep for AI · Spark Performance

Difficulty: Easy · Medium · Hard · Learn · All problems · dbt practice