Find Bursty Behavior - Problem

You are a social media data analyst tasked with identifying users who exhibit bursty posting behavior during February 2024. This is a common pattern in social networks where users have periods of high activity followed by quieter times.

Bursty behavior is defined as any 7-day period where a user's posting frequency is at least twice their average weekly posting frequency for the entire month.

Given a Posts table with columns:

  • post_id (int) - Primary key
  • user_id (int) - User identifier
  • post_date (date) - When the post was made

Your task: Find all users who demonstrate bursty behavior and return their user_id, max_7day_posts (maximum posts in any 7-day period), and avg_weekly_posts (average posts per week in February).

Important: Only analyze posts from February 1-28, 2024 (exactly 4 weeks). Order results by user_id ascending.

Input & Output

example_1.sql โ€” Basic Bursty Users
$ Input: Posts = [[1,1,'2024-02-27'],[2,5,'2024-02-06'],[3,3,'2024-02-25'],[4,3,'2024-02-14'],[5,3,'2024-02-06'],[6,2,'2024-02-25']]
โ€บ Output: [[1,1,0.2500],[2,1,0.2500],[5,1,0.2500]]
๐Ÿ’ก Note: Users 1, 2, and 5 each made only 1 post in February (avg = 0.25 posts/week). Their maximum 7-day window also contains 1 post, which is โ‰ฅ 2ร—0.25 = 0.5, so they show bursty behavior. User 3 made 3 posts but max 7-day window has only 2 posts, which is < 2ร—0.75 = 1.5.
example_2.sql โ€” High Activity User
$ Input: Posts = [[1,1,'2024-02-01'],[2,1,'2024-02-02'],[3,1,'2024-02-03'],[4,1,'2024-02-04'],[5,1,'2024-02-15']]
โ€บ Output: [[1,4,1.2500]]
๐Ÿ’ก Note: User 1 made 5 posts total (avg = 1.25 posts/week). Maximum 7-day window contains 4 posts (Feb 1-4), and 4 โ‰ฅ 2ร—1.25 = 2.5, so this user shows bursty behavior.
example_3.sql โ€” No Bursty Users
$ Input: Posts = [[1,1,'2024-02-01'],[2,1,'2024-02-08'],[3,1,'2024-02-15'],[4,1,'2024-02-22']]
โ€บ Output: []
๐Ÿ’ก Note: User 1 made 4 posts evenly distributed (avg = 1.0 posts/week). Maximum 7-day window contains only 1 post, which is < 2ร—1.0 = 2.0, so no bursty behavior detected.

Visualization

Tap to expand
Social Media Burst DetectionUser Timeline AnalysisFebruary 2024: 28 days timelineFeb 1Feb 14Feb 28User 1: Single Post AnalysisPostDay 277-day windowMax window: 1 post | Avg weekly: 0.25 | Bursty: 1 โ‰ฅ 0.5 โœ“User 3: Multiple Posts AnalysisDay 6Day 14Day 25Best windowMax window: 1 post | Avg weekly: 0.75 | Bursty: 1 < 1.5 โœ—๐ŸŽฏ Key Insight: SQL Window FunctionsUse RANGE BETWEEN to calculate rolling 7-day sums efficientlyCompare maximum window count with 2ร— monthly averageDatabase handles date arithmetic and optimization automatically
Understanding the Visualization
1
Timeline View
Visualize each user's posts as dots on a February timeline
2
Sliding Window
Move a 7-day window across the timeline, counting posts in each position
3
Peak Detection
Identify the window with the maximum number of posts
4
Burst Analysis
Compare peak activity to monthly average - if 2x higher, it's bursty!
Key Takeaway
๐ŸŽฏ Key Insight: SQL window functions with date ranges provide the most efficient solution for calculating rolling time-based aggregates and detecting activity patterns.

Time & Space Complexity

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

Where n is number of posts, dominated by sorting for window function

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

Space for intermediate results and window function calculations

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค post_id โ‰ค 106
  • 1 โ‰ค user_id โ‰ค 105
  • post_date is in YYYY-MM-DD format
  • Only posts from February 1-28, 2024 are considered
  • Each post_id is unique
  • Result must be ordered by user_id ascending
Asked in
Meta 45 Twitter 38 LinkedIn 32 Instagram 28 TikTok 24
43.6K Views
Medium-High Frequency
~25 min Avg. Time
1.8K 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