SQL
Lab
Google Google Photos Interview Question 10

Photo
Upload
Activity

Find users who uploaded photos on 3 consecutive days.

Table Schema

Inspect
Table

interactive
ColumnType
upload_idinteger
user_idinteger
upload_datedate

Sample Data

Input
Output

Sample Input: photo_uploads
upload_iduser_idupload_date
11012024-01-01
21012024-01-02
31012024-01-03
41022024-01-01
51022024-01-03
61032024-01-05
71042024-01-06
81042024-01-07
Expected Output
user_id
101

SQL Editor

Run
Query

postgresql
Waiting for query

user_id
101

Hints

Unlock
Clues

Hint 01: Work with distinct upload dates per user.
Hint 02: Create streak groups with ROW_NUMBER.
Hint 03: Require a group length of at least 3.

Solution

Locked
Answer

Solution is locked until you decide to reveal it. Try the editor first, then open this when you want the reference answer.

WITH distinct_days AS (
  SELECT DISTINCT user_id, upload_date
  FROM photo_uploads
),
streaks AS (
  SELECT
    user_id,
    upload_date - ROW_NUMBER() OVER (PARTITION BY user_id ORDER BY upload_date)::int AS streak_group
  FROM distinct_days
)
SELECT user_id
FROM streaks
GROUP BY user_id, streak_group
HAVING COUNT(*) >= 3;

Explanation

Step By
Step

01

Deduplicate user upload dates.

02

Normalize consecutive dates into the same group.

03

Return users with a 3 day upload streak.

Related Questions

Keep
Solving