KatInterview

Rolling-Window Post Engagement: Reach, Length Percentiles, Self-Replies

SQL & DataAuto-graded · 13 test cases
MediumAuto-gradedSQL & Datasqlaggregationpercentiles

Last updated

Tests SQL/Python analytics skills: filtering soft-deleted records, distinct-count aggregation, linear-interpolation percentiles, time-windowed grouping, and tie-breaking sort logic.

You have three in-memory tables representing a simple publishing platform.

users — one row per registered account

FieldTypeNotes
user_idintprimary key
countrystrtwo-letter country code
created_atstrISO date, e.g. "2025-08-01"

posts — one row per published article

FieldTypeNotes
post_idintprimary key
author_idintreferences users.user_id
created_atstrISO date

comments — one row per comment attempt

FieldTypeNotes
comment_idintprimary key
post_idintreferences posts.post_id
user_idintreferences users.user_id
created_atstrISO date
contentstrraw text
is_deletedboolsoft-delete flag

Given these three tables and a reference date today (ISO string), produce analytics over the 7-day window ending on today, i.e. [today − 6 days, today] inclusive.

Work only with active comments: those whose is_deleted is False, whose created_at falls inside the window, and whose post_id and user_id both exist in the provided posts and users tables (inner-join semantics).

Return a dictionary with exactly three keys:


"top_posts" — most-reached posts

Rank every post that received at least one active comment by the number of distinct commenters (unique user_id values). Return the top 3 as a list of dicts, each with:

  • post_id — the post's identifier
  • unique_commenters — count of distinct commenter user_ids
  • distinct_commenter_countries — count of distinct country values among those commenters

Tie-breaking when two posts share the same unique_commenters count:

  1. Prefer the post with the later posts.created_at date.
  2. If still tied, prefer the lower post_id.

If fewer than 3 posts have active comments, return however many exist.


"daily_percentiles" — comment length distribution by post and day

For every (created_at, post_id) pair that appears in the active comments, compute the P50 and P90 of len(content) using linear interpolation:

  • Sort the lengths for the group.
  • The value at percentile p sits at position (n − 1) × p in the sorted list (0-indexed), where n is the group size.
  • Interpolate linearly between the two surrounding integers when the position is not whole.

Return a list of dicts sorted by (day, post_id) ascending, each with:

  • day — the created_at date string
  • post_id
  • p50_len
  • p90_len

Only include groups that contain at least one active comment (no zero-padding for missing days).


"self_commenters" — users who replied to their own posts

Identify every user who left at least one active comment on a post they authored (i.e. comment.user_id == post.author_id). Return a list of dicts sorted by self_comment_count descending, then user_id ascending, each with:

  • user_id
  • self_comment_count — total active self-comments in the window
  • last_self_comment_at — ISO date string of the most recent such comment

Constraints

  • 1 ≤ len(users) ≤ 10 000
  • 1 ≤ len(posts) ≤ 10 000
  • 0 ≤ len(comments) ≤ 100 000
  • All created_at values are valid ISO date strings ("YYYY-MM-DD").
  • today is a valid ISO date string.
  • user_id and post_id values in comments may reference IDs that do not exist in their respective tables; treat those comments as non-existent (drop them).
  • A user may comment on the same post multiple times; each comment is an independent event.
  • For distinct_commenter_countries, count each distinct country value once regardless of how many users share it.
  • The window is fully inclusive on both ends: today − 6 days through today.

Examples

Example 1

Input: post_engagement_analytics([{'user_id': 1, 'country': 'US', 'created_at': '2025-08-01'}, {'user_id': 2, 'country': 'US', 'created_at': '2025-08-05'}, {'user_id': 3, 'country': 'CA', 'created_at': '2025-08-10'}, {'user_id': 4, 'country': 'US', 'created_at': '2025-08-20'}], [{'post_id': 10, 'author_id': 1, 'created_at': '2025-08-25'}, {'post_id': 11, 'author_id': 2, 'created_at': '2025-08-28'}, {'post_id': 12, 'author_id': 3, 'created_at': '2025-08-30'}], [{'comment_id': 100, 'post_id': 10, 'user_id': 2, 'created_at': '2025-08-26', 'content': 'Nice!', 'is_deleted': False}, {'comment_id': 101, 'post_id': 10, 'user_id': 3, 'created_at': '2025-08-27', 'content': 'Great post', 'is_deleted': False}, {'comment_id': 102, 'post_id': 11, 'user_id': 1, 'created_at': '2025-08-28', 'content': 'Thanks', 'is_deleted': False}, {'comment_id': 103, 'post_id': 11, 'user_id': 2, 'created_at': '2025-08-30', 'content': 'I agree', 'is_deleted': True}, {'comment_id': 104, 'post_id': 12, 'user_id': 4, 'created_at': '2025-08-31', 'content': 'Wow', 'is_deleted': False}, {'comment_id': 105, 'post_id': 12, 'user_id': 1, 'created_at': '2025-09-01', 'content': 'Following', 'is_deleted': False}, {'comment_id': 106, 'post_id': 12, 'user_id': 3, 'created_at': '2025-09-01', 'content': 'Awesome insights', 'is_deleted': False}, {'comment_id': 107, 'post_id': 10, 'user_id': 1, 'created_at': '2025-09-01', 'content': 'Self', 'is_deleted': False}], '2025-09-01')

Output: {'top_posts': [{'post_id': 12, 'unique_commenters': 3, 'distinct_commenter_countries': 2}, {'post_id': 10, 'unique_commenters': 3, 'distinct_commenter_countries': 2}, {'post_id': 11, 'unique_commenters': 1, 'distinct_commenter_countries': 1}], 'daily_percentiles': [{'day': '2025-08-26', 'post_id': 10, 'p50_len': 5, 'p90_len': 5}, {'day': '2025-08-27', 'post_id': 10, 'p50_len': 10, 'p90_len': 10}, {'day': '2025-08-28', 'post_id': 11, 'p50_len': 6, 'p90_len': 6}, {'day': '2025-08-31', 'post_id': 12, 'p50_len': 3, 'p90_len': 3}, {'day': '2025-09-01', 'post_id': 10, 'p50_len': 4, 'p90_len': 4}, {'day': '2025-09-01', 'post_id': 12, 'p50_len': 12.5, 'p90_len': 15.3}], 'self_commenters': [{'user_id': 1, 'self_comment_count': 1, 'last_self_comment_at': '2025-09-01'}, {'user_id': 3, 'self_comment_count': 1, 'last_self_comment_at': '2025-09-01'}]}

Hints

Hint 1

Build a single filtered list of active comments first (in-window, not deleted, both post and user exist), then derive all three outputs from it — this avoids re-scanning the raw data multiple times.

Hint 2

For top_posts, use a dict keyed by post_id to accumulate a set of commenter user_ids; then compute country cardinality by looking up each uid in your user map. The sort key needs three components: (-unique_commenters, -post_created_at_as_ordinal, post_id).

Hint 3

The linear-interpolation percentile formula is: position = (n-1) * p; result = sorted_values[floor(position)] + (sorted_values[ceil(position)] - sorted_values[floor(position)]) * fractional_part. For a single-element group, P50 == P90 == that element.