KatInterview

Banner CTR and Session Change Analysis in Pandas

SQL & DataModel answer
MediumModel answerSQL & Datapandasmerge_asofsessionization

Last updated

Clean a click-stream with pandas to compute daily banner CTR without bots or accidental clicks, user-level CTR by signup cohort, and per-user session-length change around a feature launch.

Banner and Story Metrics with Pandas

You are analysing event logs for a social app. Use pandas only, with no Python loops over rows. Treat "today" as 2025-09-01, and define "the last 7 days" as 2025-08-26 through 2025-09-01 inclusive (UTC).

Inputs

events

columntypenotes
event_idintunique, increases with time
user_idint
tsUTC datetime
event_typestringone of banner_impression, banner_click, story_view, story_post, back_navigation
banner_idnullable intset for banner events
story_idnullable intset for story events
group_idnullable intset for group story posts
dwell_msnullable inttime spent after a click

users

columntype
user_idint
is_botbool
signup_dtdate

Sample of events (event_id, user_id, ts, event_type, banner_id, story_id, group_id, dwell_ms):

1, 10, 2025-08-31T12:00:00Z, banner_impression, 7,    null, null, null
2, 10, 2025-08-31T12:00:01Z, banner_click,      7,    null, null, 200
3, 10, 2025-08-31T12:00:02Z, back_navigation,   null, null, null, null
4, 11, 2025-08-31T13:00:00Z, banner_impression, 7,    null, null, null
5, 11, 2025-08-31T13:00:05Z, banner_click,      7,    null, null, 1200
6, 12, 2025-08-29T09:00:00Z, story_post,        null, 101,  5,    null

Tasks

  1. Accidental clicks. A banner_click is accidental if dwell_ms < 500, or if the same user has a back_navigation event within 2 seconds after the click. Accidental clicks must not count toward CTR.
  2. Daily banner CTR. For the last 7 days, excluding bot users and accidental clicks, output one row per (date, banner_id) with date, banner_id, impressions, valid_clicks, ctr.
  3. User-level CTR. Compute each user's CTR over the same 7 days with the same exclusions, then summarise its distribution by signup cohort (the week of signup_dt).
  4. Session change for the Group Story feature. Compare a pre-period (2025-08-18 to 2025-08-24) with a post-period (2025-08-25 to 2025-09-01). A session is a run of a user's events (any type) with no gap longer than 30 minutes. Output user_id, pre_avg_minutes, post_avg_minutes, delta_minutes, stories_posted_change.
  5. Engineering quality. Use vectorised operations (groupby, window functions, merge_asof), keep dtypes stable, and avoid SettingWithCopyWarning. Explain which indices you would set and how you would verify correctness on the sample above.

Write the code and explain your reasoning, including the assumptions you make for ambiguous cases.

Hints

Hint 1

Clean and sort the whole event table once (tz-aware UTC timestamps, nullable integer IDs, stable sort by timestamp then event_id) before any windowing, because merge_asof needs a sorted key and a click near midnight may be followed by a back-navigation on the next day.

Hint 2

Match each click to the next back_navigation by the same user using merge_asof with by='user_id', direction='forward' and a 2-second tolerance. Handle null dwell_ms explicitly so that a missing value does not count as a short dwell.

Hint 3

Use a half-open window [2025-08-26, 2025-09-02) so the last day is included. Count impressions and valid clicks separately, then align them. For sessions, flag a new session when the gap to the user's previous event exceeds 30 minutes, take a cumulative sum for session IDs, and assign each session to the period that contains its start.