Article Views II - Problem

๐Ÿ” Article Views Tracker

Imagine you're building an analytics dashboard for a popular blog platform! You have a database table called Views that tracks every time someone reads an article.

Your Mission: Find all the people who are power readers - those who viewed more than one article on the same date.

The Views table contains:

  • article_id: The ID of the article that was viewed
  • author_id: The ID of the person who wrote the article
  • viewer_id: The ID of the person who viewed the article
  • view_date: The date when the view occurred

Key Details:

  • The table may contain duplicate rows (same person viewing same article multiple times)
  • When author_id equals viewer_id, it means the author viewed their own article
  • Return results sorted by viewer_id in ascending order

Input & Output

example_1.sql โ€” Basic Case
$ Input: Views table: +------------+-----------+-----------+------------+ | article_id | author_id | viewer_id | view_date | +------------+-----------+-----------+------------+ | 1 | 3 | 5 | 2019-08-01 | | 3 | 4 | 5 | 2019-08-01 | | 1 | 3 | 6 | 2019-08-02 | | 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 | +------------+-----------+-----------+------------+
โ€บ Output: +------+ | id | +------+ | 5 | +------+
๐Ÿ’ก Note: Viewer 5 viewed articles 1 and 3 on 2019-08-01, making them the only person who viewed more than one article on the same date. Note that viewer 4 viewed article 3 twice on 2019-07-21, but this counts as only one distinct article.
example_2.sql โ€” Multiple Power Readers
$ Input: Views table: +------------+-----------+-----------+------------+ | article_id | author_id | viewer_id | view_date | +------------+-----------+-----------+------------+ | 1 | 1 | 2 | 2023-01-01 | | 2 | 1 | 2 | 2023-01-01 | | 3 | 2 | 3 | 2023-01-01 | | 4 | 2 | 3 | 2023-01-01 | | 5 | 3 | 4 | 2023-01-02 | +------------+-----------+-----------+------------+
โ€บ Output: +------+ | id | +------+ | 2 | | 3 | +------+
๐Ÿ’ก Note: Both viewer 2 and viewer 3 viewed multiple articles on the same date (2023-01-01). Viewer 4 only viewed one article, so they don't qualify.
example_3.sql โ€” No Power Readers
$ Input: Views table: +------------+-----------+-----------+------------+ | article_id | author_id | viewer_id | view_date | +------------+-----------+-----------+------------+ | 1 | 1 | 1 | 2023-01-01 | | 1 | 1 | 1 | 2023-01-01 | | 2 | 2 | 2 | 2023-01-02 | | 3 | 3 | 3 | 2023-01-03 | +------------+-----------+-----------+------------+
โ€บ Output: +------+ | id | +------+ +------+
๐Ÿ’ก Note: No viewer read more than one distinct article on any single date. Viewer 1 viewed article 1 twice on the same date, but this counts as only one distinct article.

Visualization

Tap to expand
๐Ÿ“š Library RecordsReader 1: Book A (Mon)Reader 1: Book B (Mon)Reader 2: Book C (Mon)๐Ÿ“Š Grouped Data(Reader1, Mon): 2 books(Reader2, Mon): 1 book๐Ÿ† Power ReadersReader 1(Read 2+ books in one day)GROUP BYFILTER >1
Understanding the Visualization
1
Track All Readings
Every time someone checks out a book, we record: reader ID, book ID, and date
2
Group by Reader & Date
Organize records into groups for each (reader, date) combination
3
Count Distinct Books
For each group, count how many different books were read
4
Find Power Readers
Identify readers who read more than one book on any single day
Key Takeaway
๐ŸŽฏ Key Insight: Instead of comparing every record with every other record (expensive), we group records by (reader, date) and count distinct books in each group - much more efficient!

Time & Space Complexity

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

Single pass to group data O(n), plus sorting for GROUP BY O(n log n)

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

Space for grouping intermediate results, proportional to number of unique (viewer, date) combinations

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค Views table rows โ‰ค 105
  • article_id, author_id, viewer_id are positive integers
  • view_date is in YYYY-MM-DD format
  • The table may contain duplicate rows
  • Results must be sorted by viewer_id in ascending order
Asked in
Facebook 35 Amazon 28 Google 22 Microsoft 18
28.5K Views
Medium Frequency
~15 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