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 Name | Type |
|---|---|
| article_id | int |
| author_id | int |
| viewer_id | int |
| view_date | date |
Key Points:
- ๐ The table may contain duplicate rows (no primary key)
- ๐ค When
author_idequalsviewer_id, it means the author viewed their own article - ๐ Return the result sorted by
idin 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
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
โก Linearithmic
Space Complexity
O(k)
Space for storing unique author IDs (k is much smaller than n)
โ 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
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code