KatInterview

Top Content Category by Country and Weekly Power-User Share

SQL & DataModel answer
MediumModel answerSQL & Datasqlwindow-functionsaggregation

Last updated

Write SQL to find each country's top-impression category and the share of weekly active users who were active on 4+ days and used 3+ distinct features on a single day.

Context

You are an analyst at a worldwide content platform. The team wants a view of how impressions and engagement break down by country over the most recent week. You have three tables.

impressions (assume already limited to the week under study)

user_idcountrycategoryimpression_cnt
1USSports120
2USNews80
3CNGames200

user_activity (one row per user per day on which they were active; duplicates are possible)

user_idactivity_dt
12023-09-01
12023-09-02
12023-09-04
22023-09-01
32023-09-05

feature_usage (one row per feature used by a user on a given day)

user_idusage_dtfeature
12023-09-02search
12023-09-02share
12023-09-02save
22023-09-03search
32023-09-05save

Tasks

(a) For every country, return the content category with the largest total impressions, along with that total.

(b) Compute the weekly power-user rate. A power user is someone who:

  1. was active on at least 4 distinct days within the last 7 days, and
  2. on at least one of those active days, used at least 3 distinct features.

The rate is power users divided by users who were active at least once in the window.

What to deliver

  • Two SQL queries (any mainstream dialect is fine).
  • A short list of the assumptions you made (window definition, tie handling, denominator, NULLs).
  • A note on how you avoid inflated counts when combining tables that have many rows per user.
  • Expected output on the sample data, assuming the window ends on 2023-09-07.

Hints

Hint 1

For (a), sum impressions to the (country, category) level first, then rank within each country using a window function partitioned by country.

Hint 2

For (b), do not join user_activity and feature_usage directly and count. Reduce each to its natural grain first: distinct (user, day) pairs, and per-(user, day) distinct feature counts.

Hint 3

Compute two user-level flags separately (4+ active days; at least one active day with 3+ distinct features), then combine them with a LEFT JOIN and divide by weekly active users, guarding against a zero denominator.