Compute 7-Day Rolling Average of Unique Post Viewers
Company: TikTok
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: Medium
Interview Round: Onsite
POST_VIEWS
+---------+------------+---------+
| user_id | view_date | post_id |
| 101 | 2023-08-01 | 10 |
| 102 | 2023-08-01 | 11 |
| 101 | 2023-08-02 | 10 |
| 103 | 2023-08-03 | 12 |
| 104 | 2023-08-03 | 10 |
POSTS
+---------+----------------------+-------------------------+
| post_id | content | hashtags |
| 10 | 'Apple launch video' | '#Apple #iPhone #Launch'|
| 11 | 'Recipe tutorial' | '#Food #Recipe' |
| 12 | 'Travel vlog' | '#Travel #Adventure' |
| 13 | 'Tech news' | '#Tech #Apple' |
| 14 | 'Fitness tips' | '#Health' |
##### Scenario
A social-media analytics team has two tables: POST_VIEWS records every user’s daily post views, and POSTS stores post metadata including a free-text hashtags field.
##### Question
Write an SQL query to compute, for every post, the 7-day rolling average of daily unique viewers ordered by view_date. Given a search term (e.g. 'Apple'), return all post_id values whose hashtags column contains that term (case-insensitive). State the logical execution order of SQL clauses (e.g. FROM, WHERE, GROUP BY…) and explain why knowing this order matters when debugging or optimizing queries.
##### Hints
Use COUNT(DISTINCT user_id) over a 7-day RANGE window; apply ILIKE or LOWER(hashtags) LIKE '%apple%'.
Quick Answer: This question evaluates proficiency with SQL data manipulation, window functions for rolling aggregates, deduplication of user events, and text-filtering of metadata.