KatInterview

Distinct Content Categories per Reader

SQL & DataAuto-graded · 20 test cases
MediumAuto-gradedSQL & Datagroupingaggregationhistogram

Last updated

Tests two-level aggregation: counting distinct content categories per reader on a target date, then bucketing those counts into a frequency histogram across all dates.

Distinct Content Categories per Reader

You are given two datasets describing reader activity on a content platform.

Dataset 1: read_events — one row per reading event.

FieldTypeDescription
user_idintReader who opened the content
content_idintPiece of content that was opened
read_datestrCalendar date of the event ("YYYY-MM-DD")

Dataset 2: content_catalog — maps each piece of content to a category.

FieldTypeDescription
content_idintContent identifier
categorystrCategory the content belongs to

Every content_id in read_events is guaranteed to appear in content_catalog.

If a reader opens several pieces of content that all belong to the same category, that category is still counted only once for that reader.


Part 1 — Snapshot for a single date

For the date "2019-01-01", compute how many distinct categories each reader encountered. Return one row per reader who had at least one reading event on that date.

Output format: a list of [user_id, num_categories], sorted ascending by user_id.

Part 2 — Histogram across all dates

Using every reading event in the dataset (no date filter), compute each reader's total number of distinct categories across their full history. Then build a histogram: for each observed value of num_categories, count how many readers have exactly that value.

Output format: a list of [num_categories, num_readers], sorted ascending by num_categories. Only include values of num_categories that at least one reader has; do not emit zero-filled buckets.


Return value

Return a single dict with two keys:

{
  "daily":     [[user_id, num_categories], ...],   # Part 1
  "histogram": [[num_categories, num_readers], ...]  # Part 2
}

Constraints

  • 1 <= len(read_events) <= 10^5
  • 1 <= len(content_catalog) <= 10^4
  • user_id and content_id are positive integers.
  • read_date is a string in "YYYY-MM-DD" format.
  • Multiple rows in read_events may share the same (user_id, content_id, read_date) triple (duplicate events); they do not inflate the distinct-category count.
  • A reader absent from "2019-01-01" does not appear in the "daily" output but is included in "histogram" if they have events on other dates.
  • The number of distinct categories across all content is small relative to the number of events.

Examples

Example 1

Input: count_distinct_categories([[1, 101, '2019-01-01'], [1, 102, '2019-01-01'], [1, 103, '2019-01-02'], [2, 101, '2019-01-01'], [2, 103, '2019-01-03'], [3, 104, '2019-01-02']], [[101, 'news'], [102, 'news'], [103, 'technology'], [104, 'news']])

Output: {'daily': [[1, 1], [2, 1]], 'histogram': [[1, 1], [2, 2]]}

Hints

Hint 1

Build a lookup table from content_catalog first (content_id → category). This plays the role of a JOIN and lets you process read_events in a single pass.

Hint 2

For Part 1, track a set of categories per user, but only for rows where read_date equals '2019-01-01'. For Part 2, do the same thing with no date filter — a user's all-time set must include every date, not just the target date.

Hint 3

The histogram is an aggregation of an aggregation. First reduce each user to the size of their all-time set (one integer per user), then count how many users share each integer value. Trying to do both in one pass is the most common mistake.