cswork / libraryOpen practice workspace

Coding questions · LinkedIn

Compute article-type diversity per user and histogram

This question evaluates data manipulation and aggregation skills, including joining event logs with metadata, deduplicating by categorical attribute, date-based filtering, and computing per-user distinct counts.

You track article views and article metadata.

Tables

article_views

  • user_id INT
  • article_id INT
  • view_date DATE

articles

  • article_id INT (PK)
  • article_type VARCHAR

Tasks

  1. For each user, compute the number of distinct article types they viewed on 2019-01-01.
  • Output columns: user_id, num_article_types.
  1. Build a histogram of num_article_types on 2019-01-01 (i.e., how many users viewed 1 type, 2 types, etc.).
  • Output columns: num_article_types, num_users.

Notes

  • If a user viewed multiple articles of the same type, it counts once.
  • Users with zero views on 2019-01-01 can be excluded unless otherwise specified.

Compute article-type diversity per user and histogram

Table article_views

columntypeconstraints
user_idINTEGER
article_idINTEGER
view_dateDATE

Sample data:

article_iduser_idview_date
10112019-01-01
10212019-01-01
10312019-01-01
10122019-01-01
10322019-01-01
10432019-01-01

Table articles

columntypeconstraints
article_idINTEGERPRIMARY KEY
article_typeVARCHAR

Sample data:

article_idarticle_type
101news
102blog
103news
104review

Expected output

Histogram of distinct article types viewed per user on 2019-01-01. Each row shows a distinct count of article types and how many users had that count.

num_article_typesnum_users
12
21

Hints

  1. Join article_views to articles to access article_type.
  2. Filter rows to the target date: 2019-01-01.
  3. First aggregate by user to count DISTINCT article_type, then aggregate again to build the histogram.