cswork / libraryOpen practice workspace

Coding questions · Meta

Identify Duplicate Posts by User and Date

This question evaluates a candidate's competency in data manipulation and deduplication, focusing on aggregation and handling of temporal attributes to identify repeated entries by the same user.

posts

+---------+---------+---------------------+---------------+
| post_id | user_id | created_at          | content       |
+---------+---------+---------------------+---------------+
| 101     | 1       | 2024-05-01 10:00:00 | hello world   |
| 102     | 1       | 2024-05-01 10:00:00 | hello world   |
| 103     | 2       | 2024-05-02 12:00:00 | good morning  |
| 104     | 1       | 2024-05-03 09:00:00 | hello world   |
| 105     | 2       | 2024-05-02 12:05:00 | good morning  |
+---------+---------+---------------------+---------------+
Scenario

Meta wants to detect duplicate posts so they can surface only unique content in users’ feeds.

Question

Given table posts(post_id, user_id, created_at, content), write SQL that returns user_id, content, duplicate_count for posts where the same user posted identical content on the same calendar day (duplicate_count ≥ 2).

Hints

Group by user_id, content, DATE(created_at); HAVING COUNT(*) > 1.

Detect duplicate posts by user and calendar day

Table posts

columntypeconstraints
post_idINTEGERPRIMARY KEY, NOT NULL
user_idINTEGERNOT NULL
created_atTIMESTAMPNOT NULL
contentVARCHAR(255)NOT NULL

Sample data:

contentcreated_atpost_iduser_id
hello world2024-05-01 10:00:001011
hello world2024-05-01 10:00:001021
good morning2024-05-02 12:00:001032
hello world2024-05-03 09:00:001041
good morning2024-05-02 12:05:001052

Expected output

One row per (user_id, content) for each calendar day where the same user posted identical content at least twice on that day. Returns user_id, content, and duplicate_count (the number of such posts on that day).

contentduplicate_countuser_id
hello world21
good morning22

Hints

  1. Extract the calendar day from created_at, for example using DATE(created_at).
  2. Group by user_id, content, and DATE(created_at) and filter groups with HAVING COUNT(*) >= 2.