← Databricks Interview Insights
The core join/window logic isn't too bad once you see it, but I fumbled the multi-year reset part initially.
Start by computing daily cumulative spend per customer within each year, then determine the first date each customer crosses the $1000 threshold. For each week, count distinct customers whose threshold date is on or before the week start date, ensuring the count resets annually. Finally, generate a complete weekly time series and join the counts.
Pro tip: Clarify the definition of 'week start' (e.g., Monday) and whether the count should include customers who reached the threshold in prior weeks. Also, consider using a calendar table to handle weeks with no new qualifiers, ensuring a continuous time series.
Create a table of all week start dates covering the entire date range in the data, ensuring no gaps. This will be the backbone for the time series.
For each customer and each transaction date, calculate the running total of spend within the year, resetting on January 1st. Use window functions partitioned by customer and year, ordered by date.
For each customer and year, find the earliest date where the YTD cumulative spend reaches or exceeds $1000. This is the date they become 'qualified'.
For each week start date, count distinct customers who have a threshold crossing date on or before that week start date and within the same year. Ensure the count resets annually by grouping by year.
Select year, week start date, and customer count, ordered chronologically by year and week start date. Handle weeks with zero new qualifiers by including them with the cumulative count.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.