cswork / libraryOpen practice workspace

Coding questions · Airbnb

Find Users with Seven Consecutive Impression Days

Find users with impressions on seven consecutive calendar days using PostgreSQL, accounting for duplicate days and longer activity streaks.

A user qualifies as a super user if they have at least one impression on each day of any run of seven consecutive calendar days. Find every qualifying user using PostgreSQL.

Input Table

impressions

ColumnTypeMeaning
impression_idINTEGERUnique impression identifier; primary key
user_idINTEGERNon-null user identifier
impression_dateDATENon-null calendar date of the impression

Output Contract

Write one read-only PostgreSQL query returning a single column, user_id, with each qualifying user exactly once, ordered by user_id ascending.

Example

Suppose user 10 has impressions on January 1, 2, 3, 4, 5, 6, and 7 of the same year, with two impressions on January 3. User 20 has impressions on January 1, 2, 3, 5, 6, 7, and 8.

Expected result:

user_id
10

User 20 has seven distinct impression dates, but no seven-day consecutive run because January 4 is missing.

Constraints and Clarifications

  • The exercise uses the supplied calendar dates directly; no timestamp-to-time-zone conversion is needed.
  • Several impressions on the same day count as one active day for that user.
  • A run may cross a month or year boundary.
  • A run longer than seven days qualifies, and multiple qualifying runs still produce one user row.
  • Search the entire table history; this is not a requirement to be active on the seven days immediately preceding today.

Hint: Compare consecutive active dates

Deduplicate each user's active dates before measuring runs. A streak is a property of consecutive dates, not simply the number of impression rows.

Find Super Users With Seven Consecutive Active Days

Table impressions

columntypeconstraints
impression_idINTEGERPRIMARY KEY
user_idINTEGERNOT NULL
impression_dateDATENOT NULL

Sample data:

impression_dateimpression_iduser_id
2025-03-011105
2025-03-022105
2025-03-033105
2025-03-044105
2025-03-055105
2025-03-066105
2025-03-077105
2025-03-088105
2025-03-099105
2025-03-1010105
2025-04-0111105
2025-04-0212105
2025-04-0313105
2025-04-0414105
2025-04-0515105
2025-04-0616105
2025-04-0717105
2025-01-0118101
2025-01-0219101
2025-01-0320101
2025-01-0421101
2025-01-0522101
2025-01-0623101
2025-01-0724101
2025-01-0325101
2024-12-2826104
2024-12-2927104
2024-12-3028104
2024-12-3129104
2025-01-0130104
2025-01-0231104
2025-01-0332104
2023-06-013399
2023-06-023499
2023-06-033599
2023-06-043699
2023-06-053799
2023-06-063899
2023-06-073999
2024-02-144099
2025-01-054199
2025-01-1042103
2025-01-1143103
2025-01-1244103
2025-01-1345103
2025-01-1446103
2025-01-1547103
2025-01-1148103
2025-01-1349103
2025-01-0150102
2025-01-0251102
2025-01-0352102
2025-01-0553102
2025-01-0654102
2025-01-0755102
2025-01-0856102
2025-01-0157106
2025-01-0258106
2025-01-0259106
2025-01-0460106

Expected output

user_id
99
101
104
105

Hints

  1. Deduplicate each user's active dates before measuring runs. A streak is a property of consecutive dates, not simply the number of impression rows.