Article Views I - Problem

Given a table Views that tracks article views, find all authors who viewed at least one of their own articles.

Table: Views

Column NameType
article_idint
author_idint
viewer_idint
view_datedate

Key Points:

  • No primary key exists, duplicate rows are possible
  • Each row represents a viewer looking at an article on a specific date
  • When author_id = viewer_id, the author viewed their own article
  • Return results sorted by id in ascending order

Table Schema

Views
Column Name Type Description
article_id int ID of the article being viewed
author_id int ID of the article's author
viewer_id int ID of the person viewing the article
view_date date Date when the article was viewed
Note: No primary key exists, duplicate rows are possible. When author_id = viewer_id, the author viewed their own article.

Input & Output

Example 1 — Mixed Self and Other Views
Input Table:
article_id author_id viewer_id view_date
1 1 1 2019-08-01
2 1 2 2019-08-01
1 4 1 2019-07-22
3 4 4 2019-07-21
3 4 4 2019-07-21
Output:
id
1
4
💡 Note:

Author 1 viewed their own article (article_id=1) and author 4 viewed their own article (article_id=3). The duplicate row for author 4 is handled by DISTINCT. Author 1 also had someone else view their article, but that doesn't affect the result.

Example 2 — No Self Views
Input Table:
article_id author_id viewer_id view_date
1 1 2 2019-08-01
2 2 1 2019-08-02
Output:
id
💡 Note:

No authors viewed their own articles. Author 1's article was viewed by author 2, and author 2's article was viewed by author 1, but neither viewed their own work.

Example 3 — Single Self View
Input Table:
article_id author_id viewer_id view_date
5 3 3 2019-08-01
Output:
id
3
💡 Note:

Author 3 viewed their own article (article_id=5), so they appear in the result.

Constraints

  • 1 ≤ article_id, author_id, viewer_id ≤ 1000
  • view_date is a valid date
  • The table may contain duplicate rows

Visualization

Tap to expand
Article Views I - Self-View Detection INPUT: Views Table article author viewer date 1 3 3 2019-08-01 1 3 1 2019-08-01 2 7 7 2019-08-01 2 7 6 2019-08-02 4 7 1 2019-07-22 3 4 4 2019-07-21 3 4 4 2019-07-21 Blue = author_id equals viewer_id author_id = viewer_id Self-view condition ALGORITHM STEPS 1 Filter Condition WHERE author_id = viewer_id 2 Select Author ID SELECT author_id AS id 3 Remove Duplicates DISTINCT keyword 4 Sort Results ORDER BY id ASC SELECT DISTINCT author_id AS id FROM Views WHERE author_id = viewer_id ORDER BY id FINAL RESULT Authors who viewed their own articles id (author_id) 3 4 7 Verification: Author 3 viewed article 1 OK Author 4 viewed article 3 OK Author 7 viewed article 2 OK Sorted ascending: 3, 4, 7 Key Insight: The core condition author_id = viewer_id identifies self-views. DISTINCT removes duplicate entries (e.g., author 4 appears twice but counts once). Simple equality check makes this O(n) scan efficient. TutorialsPoint - Article Views I | Optimal Solution Data Process Output
Asked in
Facebook 28 Amazon 15
28.5K Views
High Frequency
~8 min Avg. Time
892 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