Quick Overview

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

Find Users with Seven Consecutive Impression Days

Company: Airbnb

Role: Data Engineer

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Onsite

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` | Column | Type | Meaning | | --- | --- | --- | | `impression_id` | INTEGER | Unique impression identifier; primary key | | `user_id` | INTEGER | Non-null user identifier | | `impression_date` | DATE | Non-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. ```

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

Read the full Airbnb Data Engineer interview experience this question came from

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`: - `impression_id` INTEGER: unique impression identifier; primary key - `user_id` INTEGER: non-null user identifier - `impression_date` DATE: non-null calendar date of the impression Write one read-only PostgreSQL query returning a single column, `user_id`, with each qualifying user exactly once, ordered by `user_id` ascending. Clarifications: - Use 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. Example: if user 10 has impressions on January 1, 2, 3, 4, 5, 6, and 7 of the same year (two on January 3) and user 20 has impressions on January 1, 2, 3, 5, 6, 7, and 8, only user 10 qualifies; user 20 has seven distinct impression dates but no seven-day consecutive run because January 4 is missing.

Tables

impressions(impression_id INTEGER, user_id INTEGER, impression_date DATE)

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.

Loading coding console...