Find users who posted identical content at least four times within one hour

Quick Overview

Write a SQL query over a posts table that finds users who posted identical content at least four times inside any one-hour window. It tests self-joins or range window frames, sliding time windows versus fixed calendar buckets, and inclusive boundary handling.

Find users who posted identical content at least four times within one hour

Company: Reddit

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

Each row of `posts` is one post a user published. Find users who posted the same content more than 3 times within a one-hour window. A `(user_id, content)` pair qualifies when there is a post such that at least 3 other posts by the same user, with identical content, were created within 1 hour after it (both ends inclusive). Return each qualifying `(user_id, content)` pair once. ```hint Where the window starts If four identical posts fit inside one hour, think about which of the four you could treat as the start of that hour, and what you would count from it. ``` ### Input Table(s) `posts` | column | type | meaning | |---|---|---| | `post_id` | INTEGER | Unique id of the post. Primary key. | | `user_id` | INTEGER | Id of the user who wrote the post. Not null. | | `content` | VARCHAR(500) | Text of the post. Not null, non-empty. | | `created_at` | TIMESTAMP | When the post was created. Not null. | Key: `post_id` is the primary key. A user can have many posts, and the same content can appear many times. ### Output Contract - Columns, in this order: `user_id`, `content`. - One row per qualifying `(user_id, content)` pair, with no repeats. - Sort by `user_id` ascending, then `content` ascending. The pair is unique in the output, so this order is fully determined. - No rounding is involved. ### Example `posts` | post_id | user_id | content | created_at | |---|---|---|---| | 1 | 10 | buy now | 2024-03-01 10:00:00 | | 2 | 10 | buy now | 2024-03-01 10:20:00 | | 3 | 10 | buy now | 2024-03-01 10:40:00 | | 4 | 10 | buy now | 2024-03-01 11:00:00 | | 5 | 10 | hello | 2024-03-01 10:05:00 | | 6 | 20 | spam link | 2024-03-01 09:00:00 | | 7 | 20 | spam link | 2024-03-01 09:30:00 | | 8 | 20 | spam link | 2024-03-01 10:00:00 | | 9 | 20 | spam link | 2024-03-01 10:01:00 | | 10 | 20 | spam link | 2024-03-01 10:20:00 | | 11 | 30 | buy now | 2024-03-01 10:00:00 | | 12 | 30 | buy now | 2024-03-01 10:30:00 | | 13 | 30 | buy now | 2024-03-01 11:00:00 | | 14 | 30 | buy now | 2024-03-01 11:30:00 | Expected result: | user_id | content | |---|---| | 10 | buy now | | 20 | spam link | User 10 posted "buy now" at 10:00, 10:20, 10:40 and 11:00. Post 4 is exactly one hour after post 1, and the boundary is inclusive, so the pair qualifies. User 20's first post at 09:00 has only two matching posts within the next hour, but the post at 09:30 has three (10:00, 10:01 and 10:20), so the pair qualifies through a window that does not start at the user's first post. User 30 also posted "buy now" four times, but those posts span 90 minutes and no one-hour window holds all four. User 10's single "hello" post does not qualify. ### Constraints and Clarifications - `posts` has at most 10,000 rows. - No column contains NULL. - Content must match exactly to count as the same. Posts by different users never combine, even when the text is identical. - "More than 3 times" means at least 4 posts with the same content by the same user, all inside some one-hour window. The window is inclusive at both ends: a post exactly 60 minutes after the first post of the window counts. - The window slides. It can start at any of the user's posts, not only at the top of a calendar hour. - Two posts with the same `created_at` are still two separate posts and both count. - A pair that qualifies through several different windows appears once in the result.

Overview: Write a SQL query over a posts table that finds users who posted identical content at least four times inside any one-hour window. It tests self-joins or range window frames, sliding time windows versus fixed calendar buckets, and inclusive boundary handling.

Reddit logo
Reddit
Nov 24, 2026
mediumData ScientistOtherData Manipulation (SQL/Python)
0
0

Each row of posts is one post a user published. Find users who posted the same content more than 3 times within a one-hour window. A (user_id, content) pair qualifies when there is a post such that at least 3 other posts by the same user, with identical content, were created within 1 hour after it (both ends inclusive). Return each qualifying (user_id, content) pair once.

Input Table(s)

posts

columntypemeaning
post_idINTEGERUnique id of the post. Primary key.
user_idINTEGERId of the user who wrote the post. Not null.
contentVARCHAR(500)Text of the post. Not null, non-empty.
created_atTIMESTAMPWhen the post was created. Not null.

Key: post_id is the primary key. A user can have many posts, and the same content can appear many times.

Output Contract

  • Columns, in this order: user_id , content .
  • One row per qualifying (user_id, content) pair, with no repeats.
  • Sort by user_id ascending, then content ascending. The pair is unique in the output, so this order is fully determined.
  • No rounding is involved.

Example

posts

post_iduser_idcontentcreated_at
110buy now2024-03-01 10:00:00
210buy now2024-03-01 10:20:00
310buy now2024-03-01 10:40:00
410buy now2024-03-01 11:00:00
510hello2024-03-01 10:05:00
620spam link2024-03-01 09:00:00
720spam link2024-03-01 09:30:00
820spam link2024-03-01 10:00:00
920spam link2024-03-01 10:01:00
1020spam link2024-03-01 10:20:00
1130buy now2024-03-01 10:00:00
1230buy now2024-03-01 10:30:00
1330buy now2024-03-01 11:00:00
1430buy now2024-03-01 11:30:00

Expected result:

user_idcontent
10buy now
20spam link

User 10 posted "buy now" at 10:00, 10:20, 10:40 and 11:00. Post 4 is exactly one hour after post 1, and the boundary is inclusive, so the pair qualifies. User 20's first post at 09:00 has only two matching posts within the next hour, but the post at 09:30 has three (10:00, 10:01 and 10:20), so the pair qualifies through a window that does not start at the user's first post. User 30 also posted "buy now" four times, but those posts span 90 minutes and no one-hour window holds all four. User 10's single "hello" post does not qualify.

Constraints and Clarifications

  • posts has at most 10,000 rows.
  • No column contains NULL.
  • Content must match exactly to count as the same. Posts by different users never combine, even when the text is identical.
  • "More than 3 times" means at least 4 posts with the same content by the same user, all inside some one-hour window. The window is inclusive at both ends: a post exactly 60 minutes after the first post of the window counts.
  • The window slides. It can start at any of the user's posts, not only at the top of a calendar hour.
  • Two posts with the same created_at are still two separate posts and both count.
  • A pair that qualifies through several different windows appears once in the result.