← Pinterest Interview Insights
The tie-breaking rule tripped me up a bit.
Break the problem into two parts: first, identify each user's first appointment and check if it was cancelled; second, for those users, determine if they ever had a returned or confirmed appointment after that first appointment. Then compute the percentage of users who never had such a subsequent appointment.
Pro tip: Clarify the definition of 'returned or confirmed' statuses and consider edge cases like multiple first appointments or ties in appointment dates. Also, discuss how you would handle users with no subsequent appointments in the denominator.
Use a window function like ROW_NUMBER() partitioned by user_id and ordered by appointment date to find each user's first appointment. Ensure you handle ties appropriately.
From the first appointments, select only those where status = 'cancelled'. This gives the cohort of users for the analysis.
For each user in the cohort, check if they have any appointment after their first appointment with status in ('returned', 'confirmed'). Use a LEFT JOIN or EXISTS subquery.
Count the number of users in the cohort who have no such subsequent appointment, divide by the total number of users in the cohort, and multiply by 100 to get the percentage.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.