Article Views I - Problem

Welcome to the world of content analytics! ๐Ÿ“Š

You're working as a data analyst for a popular online publishing platform. Your task is to identify narcissistic authors - those who read their own articles! ๐Ÿ“

Given a Views table that tracks every article view, you need to find all authors who have viewed at least one of their own articles.

Table Structure:

Column NameType
article_idint
author_idint
viewer_idint
view_datedate

Key Points:

  • ๐Ÿ”„ The table may contain duplicate rows (no primary key)
  • ๐Ÿ‘ค When author_id equals viewer_id, it means the author viewed their own article
  • ๐Ÿ“ˆ Return the result sorted by id in ascending order

Goal: Find all unique author IDs who have been caught reading their own content!

Input & Output

example_1.sql โ€” Basic Case
$ Input: Views table: +------------+-----------+-----------+------------+ | article_id | author_id | viewer_id | view_date | +------------+-----------+-----------+------------+ | 1 | 3 | 5 | 2019-08-01 | | 1 | 3 | 3 | 2019-08-01 | | 2 | 7 | 7 | 2019-08-01 | | 2 | 7 | 6 | 2019-08-02 | +------------+-----------+-----------+------------+
โ€บ Output: +------+ | id | +------+ | 3 | | 7 | +------+
๐Ÿ’ก Note: Author 3 viewed their own article (article 1), and author 7 viewed their own article (article 2). Author 3 and 7 are the narcissistic authors!
example_2.sql โ€” With Duplicates
$ Input: Views table: +------------+-----------+-----------+------------+ | article_id | author_id | viewer_id | view_date | +------------+-----------+-----------+------------+ | 1 | 3 | 3 | 2019-08-01 | | 1 | 3 | 3 | 2019-08-01 | | 2 | 5 | 8 | 2019-08-02 | +------------+-----------+-----------+------------+
โ€บ Output: +------+ | id | +------+ | 3 | +------+
๐Ÿ’ก Note: Even though author 3 has duplicate self-view records, DISTINCT ensures they appear only once in the result.
example_3.sql โ€” No Self-Views
$ Input: Views table: +------------+-----------+-----------+------------+ | article_id | author_id | viewer_id | view_date | +------------+-----------+-----------+------------+ | 1 | 3 | 5 | 2019-08-01 | | 2 | 7 | 6 | 2019-08-02 | | 3 | 4 | 1 | 2019-08-03 | +------------+-----------+-----------+------------+
โ€บ Output: Empty result set (no rows)
๐Ÿ’ก Note: No author viewed their own article, so the result is empty. All authors were modest!

Visualization

Tap to expand
๐Ÿ“š Digital Library LogbookArticle | Author | Viewer | Date๐Ÿ“„ Article 1 | Author 3 | Reader 5 | Aug 1๐Ÿ“„ Article 1 | Author 3 | Reader 3 | Aug 1 ๐Ÿ•ต๏ธ๐Ÿ“„ Article 2 | Author 7 | Reader 7 | Aug 1 ๐Ÿ•ต๏ธ๐Ÿ“„ Article 2 | Author 7 | Reader 6 | Aug 2๐Ÿ“„ Article 3 | Author 4 | Reader 1 | Aug 3๐Ÿ” Detective WorkSpot: Author = Viewer๐ŸŽฏ Caught Authors37Authors who readtheir own articles!๐Ÿ’ก SQL Detective QuerySELECT DISTINCT author_id as id FROM ViewsWHERE author_id = viewer_id ORDER BY id
Understanding the Visualization
1
Examine the Logbook
Look at each entry in the Views table - it shows article_id, author_id, viewer_id, and view_date
2
Spot the Pattern
When author_id equals viewer_id, you've caught an author reading their own work!
3
Collect Evidence
Gather all unique author IDs who appear as both author and viewer
4
Present Findings
Sort the caught authors by ID and present your findings
Key Takeaway
๐ŸŽฏ Key Insight: This is a simple filtering problem! Just look for rows where `author_id = viewer_id` - no complex joins needed. The DISTINCT keyword handles duplicates, and ORDER BY sorts the results.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n log n)

O(n) to scan the table + O(k log k) to sort results where k is number of unique authors

n
2n
โšก Linearithmic
Space Complexity
O(k)

Space for storing unique author IDs (k is much smaller than n)

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค Views table rows โ‰ค 105
  • 1 โ‰ค article_id, author_id, viewer_id โ‰ค 109
  • view_date is a valid date
  • The table may contain duplicate rows
Asked in
Google 15 Amazon 12 Meta 8 Microsoft 6
23.4K Views
High Frequency
~8 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen