KatInterview

Social Media Reply Analytics

SQL & DataAuto-graded · 16 test cases
EasyAuto-gradedSQL & Datadata-analysisaggregationfiltering

Last updated

Analyzes reply patterns in social media data using temporal filtering, joins, and aggregation to compute engagement metrics.

You are building analytics for a social media platform. The platform has posts and replies stored in two data structures:

Data Structures

all_post (list of dictionaries)

Each dictionary represents a post or reply with these keys:

  • post_id: integer, unique identifier
  • post_author_id: integer, ID of the user who created this content
  • post_creation_time: string (ISO 8601 format with 'Z' suffix) or datetime object
  • post_type: string, either 'post' (original content) or 'reply' (response to another post)
  • post_content: string, the text content
  • post_parent_id: integer or None
    • For replies (post_type='reply'), this points to the post_id being replied to
    • For original posts (post_type='post'), this is None

users (list of dictionaries)

Each dictionary represents a user:

  • user_id: integer, unique identifier
  • age: integer
  • country: string (two-letter code like 'US', 'CA')

Your Task

Implement a function that returns a dictionary with two metrics:

Metric 1: num_users

Count how many distinct users authored at least one original post that received 2 or more replies within a 7-day window.

The 7-day window is inclusive on both ends: a reply counts if its post_creation_time falls between the original post's timestamp and that timestamp plus exactly 7 days.

Metric 2: pct_users

Among users who received at least one reply to any of their original posts, calculate what percentage received replies from at least 2 distinct users who are both located in the US (country='US').

  • Numerator: count of users whose original posts received replies from 2+ distinct US-based repliers
  • Denominator: count of users who received at least one reply to any original post
  • Result: percentage as a float between 0 and 100

If no users received any replies, return 0.0 for this metric.

Return Value

A dictionary with two keys:

  • 'num_users': integer
  • 'pct_users': float

Constraints

  • 1 ≤ len(all_post) ≤ 10,000
  • 0 ≤ len(users) ≤ 5,000
  • All post_id values are unique
  • All user_id values are unique
  • Timestamps are either datetime objects or ISO 8601 strings with 'Z' timezone
  • A reply's post_parent_id may reference a post not in the dataset (orphan reply)
  • Users may reply to their own posts
  • The same user may reply multiple times to the same post

Examples

Example 1

Input: analyze_reply_metrics([{'post_id': 1, 'post_author_id': 10, 'post_creation_time': '2024-01-01T00:00:00Z', 'post_type': 'post', 'post_content': 'hello', 'post_parent_id': None}, {'post_id': 2, 'post_author_id': 20, 'post_creation_time': '2024-01-02T00:00:00Z', 'post_type': 'reply', 'post_content': 'reply one', 'post_parent_id': 1}, {'post_id': 3, 'post_author_id': 21, 'post_creation_time': '2024-01-08T00:00:00Z', 'post_type': 'reply', 'post_content': 'reply two', 'post_parent_id': 1}, {'post_id': 4, 'post_author_id': 22, 'post_creation_time': '2024-01-09T00:00:00Z', 'post_type': 'reply', 'post_content': 'late', 'post_parent_id': 1}], [{'user_id': 10, 'age': 30, 'country': 'CA'}, {'user_id': 20, 'age': 25, 'country': 'US'}, {'user_id': 21, 'age': 31, 'country': 'US'}, {'user_id': 22, 'age': 28, 'country': 'CA'}])

Output: {'num_users': 1, 'pct_users': 100}

Example 2

Input: analyze_reply_metrics([{'post_id': 1, 'post_author_id': 10, 'post_creation_time': '2024-03-01T12:00:00Z', 'post_type': 'post', 'post_content': 'post', 'post_parent_id': None}, {'post_id': 2, 'post_author_id': 20, 'post_creation_time': '2024-03-01T13:00:00Z', 'post_type': 'reply', 'post_content': 'reply', 'post_parent_id': 1}, {'post_id': 3, 'post_author_id': 10, 'post_creation_time': '2024-03-02T12:00:00Z', 'post_type': 'post', 'post_content': 'another', 'post_parent_id': None}, {'post_id': 4, 'post_author_id': 20, 'post_creation_time': '2024-03-03T12:00:00Z', 'post_type': 'reply', 'post_content': 'reply again', 'post_parent_id': 3}, {'post_id': 5, 'post_author_id': 30, 'post_creation_time': '2024-03-04T12:00:00Z', 'post_type': 'reply', 'post_content': 'reply third', 'post_parent_id': 3}], [{'user_id': 10, 'age': 40, 'country': 'CA'}, {'user_id': 20, 'age': 27, 'country': 'US'}, {'user_id': 30, 'age': 32, 'country': 'US'}])

Output: {'num_users': 1, 'pct_users': 100}

Hints

Hint 1

Build two indexes in a single pass: one mapping each post to its replies, and another mapping each original post author to all replies they received.

Hint 2

For the 7-day window, remember that 'within 7 days' means the reply timestamp must be ≤ original timestamp + 7 days, inclusive.

Hint 3

When counting distinct US repliers, use a set to ensure the same user replying multiple times only counts once.