KatInterview

Approved-Profile Daily Snapshot with Country Rollup and Checks

SQL & DataModel answer
MediumModel answerSQL & Datasqlwindow-functionspoint-in-time-snapshot

Last updated

Build a point-in-time snapshot of profiles whose latest version has an approved final review, add 7-day engagement and country rollups, and write data-quality and rerun-safety checks.

Scenario

A dating app wants a daily report on profile quality and engagement. The report runs for the snapshot date 2025-09-01, and all timestamps are UTC. A profile belongs in the report only if both of these hold:

  • Its newest version (as of 2025-09-01 00:00:00) has a final review with status = 'approved' made on or before that moment.
  • The profile is not deleted (is_deleted = 0).

Schema and sample data

users

idsignup_datecountry
12025-08-20US
22025-08-25US
32025-08-28CA
42025-07-15US

profiles

profile_iduser_idcreated_atis_deleted
10112025-08-21 10:00:000
10222025-08-25 09:00:000
10332025-08-28 12:30:001
10442025-07-16 08:15:000

profile_versions

version_idprofile_idcreated_atbio_textphotos_count
10011012025-08-21 10:05:00Hi2
10021012025-08-29 07:00:00Hello3
10031022025-08-25 09:05:00Traveler1
10041032025-08-28 12:35:00Foodie0
10051042025-08-20 18:00:00Hiker4

profile_reviews

review_idprofile_idversion_idstatusreviewed_atis_final
50011011001approved2025-08-21 12:00:000
50021011002approved2025-08-29 12:00:001
50031021003rejected2025-08-25 10:00:001
50041031004approved2025-08-28 14:00:001
50051041005pending2025-08-20 19:00:000

profile_events

idprofile_idevent_typeevent_time
9001101view2025-08-31 10:00:00
9002101like2025-08-31 10:05:00
9003102view2025-08-30 09:30:00
9004104view2025-08-26 12:00:00
9005104like2025-08-31 14:00:00

Assume the text values are stored without the display quotes.

Rules

  • The latest version of a profile is the one with the greatest created_at that is <= 2025-09-01 00:00:00.
  • The engagement window covers the 7 days from 2025-08-26 00:00:00 through the end of 2025-09-01 (inclusive of that whole day).

Tasks

  1. Snapshot table. Write SQL (CTEs and window functions) that produces one row per qualifying profile with: user_id, country, profile_id, latest_version_id, approved_at, photos_count, bio_len, last_7d_views, last_7d_likes, last_7d_like_rate (likes divided by views).
  2. Country rollup. From the snapshot, compute per country: profiles_approved, pct_with_photo (photos_count >= 1), pct_with_bio_10+ (LENGTH(bio_text) >= 10), last_7d_views, last_7d_likes, last_7d_like_rate.
  3. Validation queries.
    • (a) Flag profiles whose final review points at a version that is not the latest.
    • (b) Detect profiles with more than one final review.
    • (c) Surface profiles that received events after the snapshot time.
    • (d) Show how the job stays idempotent when rerun, by bounding everything to the snapshot.

Briefly explain any ambiguity you resolve and how the sample data comes out.

Hints

Hint 1

Treat each table separately: pick the newest version with created_at <= snapshot, and the newest final review with reviewed_at <= snapshot, using ROW_NUMBER() per profile with a deterministic tie-breaker on the id.

Hint 2

A profile qualifies only if its effective final review is approved AND references the latest version id. A later final rejection overrides an earlier approval, and an approval of an old version does not count.

Hint 3

Aggregate events in their own CTE and LEFT JOIN them so profiles with no events survive; compute country rates as SUM(likes)/SUM(views), not the average of per-profile rates; and drive every filter from fixed parameters, never NOW().