Hone logo
Hone
Problems

Article Views I

You are tasked with analyzing user activity on a content platform. Specifically, you need to identify which articles have been viewed by a particular user. This is a common task for personalizing content recommendations and tracking user engagement.

Problem Description

You are given a table named Views that records user views of articles. Each row in the table represents a single view and contains the following information:

  • article_id: The unique identifier of the article that was viewed.
  • author_id: The unique identifier of the author of the article.
  • viewer_id: The unique identifier of the user who viewed the article.
  • view_date: The date on which the article was viewed.

Your goal is to find the IDs of all articles that were viewed by a specific viewer.

Key Requirements:

  • Select only the article_id column from the Views table.
  • Filter the results to include only views made by a specific viewer_id.
  • Ensure that each article_id is listed only once in the output, even if the same article was viewed multiple times by the same viewer.
  • The output should be sorted by article_id in ascending order.

Expected Behavior:

The output should be a list of distinct article_ids.

Edge Cases to Consider:

  • What if the specified viewer_id has not viewed any articles?
  • What if there are no records in the Views table at all?

Examples

Example 1:

Input:
Views table:
| article_id | author_id | viewer_id | view_date  |
|------------|-----------|-----------|------------|
| 1          | 101       | 201       | 2023-10-26 |
| 2          | 102       | 202       | 2023-10-26 |
| 1          | 101       | 201       | 2023-10-27 |
| 3          | 103       | 201       | 2023-10-27 |
| 2          | 102       | 203       | 2023-10-28 |

Target viewer_id: 201

Output:
| article_id |
|------------|
| 1          |
| 3          |

Explanation:
Articles with article_id 1 and 3 were viewed by viewer_id 201. Although article_id 1 was viewed twice by viewer_id 201, it only appears once in the output. The output is sorted by article_id.

Example 2:

Input:
Views table:
| article_id | author_id | viewer_id | view_date  |
|------------|-----------|-----------|------------|
| 10         | 501       | 601       | 2023-11-01 |
| 20         | 502       | 602       | 2023-11-01 |
| 10         | 501       | 603       | 2023-11-02 |

Target viewer_id: 601

Output:
| article_id |
|------------|
| 10         |

Explanation:
Only article_id 10 was viewed by viewer_id 601.

Example 3: Edge Case - Viewer with no views

Input:
Views table:
| article_id | author_id | viewer_id | view_date  |
|------------|-----------|-----------|------------|
| 1          | 101       | 201       | 2023-10-26 |
| 2          | 102       | 202       | 2023-10-26 |

Target viewer_id: 999

Output:
| article_id |
|------------|
(empty result set)

Explanation:
The target viewer_id 999 has no corresponding entries in the Views table, so the output is empty.

Constraints

  • The Views table can contain up to 1,000,000 rows.
  • article_id, author_id, and viewer_id are integers.
  • view_date is a date type.
  • The target viewer_id will be a valid integer.
  • The solution should be efficient enough to handle the maximum number of rows within reasonable time limits (e.g., under 5 seconds).

Notes

  • Consider how to handle duplicate article views for the same viewer.
  • Think about the order of operations when filtering and selecting distinct values.
  • Pseudocode for a typical solution might involve iterating through the table, checking the viewer_id, and storing unique article_ids.
Loading editor...
plaintext