This one took me longer than I wanted to admit just to map out the dedup problem before writing a single line.
Start by clarifying metric definitions and edge cases, then outline a query structure using CTEs to deduplicate sessions, aggregate payments, and perform a full outer join to capture unmatched payments. Finally, compute daily and country-level metrics with careful handling of date ranges and nulls.
Pro tip: Explicitly state your assumptions about session deduplication (e.g., keeping the latest session per user per day) and how you handle unmatched payments, as these choices significantly impact metric accuracy and demonstrate product sense.
Confirm definitions: DAU as distinct users with sessions, payers as distinct users with payments, revenue as sum of payment amounts, ARPDAU = revenue/DAU, ARPPU = revenue/payers. Discuss how to handle duplicate sessions, multiple payments per session, and unmatched payments.
Use CTEs to deduplicate app_sessions (e.g., row_number() over partition by user_id, session_id, date) and aggregate payments per session (sum amount, count transactions) to avoid fan-out.
Perform a FULL OUTER JOIN on user_id and session_id to include unmatched payments. Use COALESCE to align dates and countries from both tables, ensuring payments without sessions are counted.
Group by date and country, then calculate DAU (distinct users from sessions), payers (distinct users from payments), revenue (sum of payment amounts), ARPDAU (revenue/DAU), and ARPPU (revenue/payers). Handle division by zero with NULLIF.
Apply a date filter (e.g., date >= current_date - interval '7 days') in the final SELECT or within CTEs, and ensure the query is efficient and readable with comments.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.