Spread Campaign Budgets Evenly Across Flight Dates to Daily Totals
Last updated: November 29, 2026
Quick Overview
Each ad campaign has (campaign_id, start_date, end_date, budget). Assuming even daily pacing, expand budgets to daily planned spend and return total planned spend per calendar day across all campaigns, with daily cents that sum exactly to each budget.
Meta
November 29, 20260
0
0 solved
Each ad campaign has (campaign_id, start_date, end_date, budget). Assuming even daily pacing, expand budgets to daily planned spend and return total planned spend per calendar day across all campaigns, with daily cents that sum exactly to each budget.
Meta ads pacing and finance forecasting need campaign budgets spread over flight days. The interviewer checks date-range expansion with explode, inclusive date arithmetic, and the subtle requirement that rounded daily amounts must still add up to the budget, which naive rounding breaks.
What the Interviewer Expects
- Generate the inclusive list of days per campaign
- Explode to one row per campaign and day
- Work in integer cents and distribute the remainder so totals reconcile
- Aggregate planned spend per day across campaigns
- Validate that each campaign's daily rows sum to its budget
Key Topics to Cover
How to Approach This
- Clarify the schema and expected output format before writing queries.
- Use CTEs (WITH clauses) to break complex queries into readable steps.
- Consider window functions (ROW_NUMBER, RANK, LAG, LEAD) for ranking and sequential analysis.
- Watch for NULLs, duplicates, and edge cases in JOINs and GROUP BY.
- For pandas, prefer vectorized operations over row-by-row iteration.
Possible Follow-up Questions
- How would you pace by expected traffic instead of evenly?
- How would you avoid materializing one row per day for year-long campaigns?
- How would you update the plan when a campaign is paused midway?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice SQL ProblemsSample Answer
Clarify the Data
Dates are inclusive. Budget is in dollars with cents. A campaign with end before start is invalid and dropped. Output: date, planned_spend in dollars, and internally every campaign's daily cents s...
Approach
Convert the budget to integer cents. For each campaign build pd.date_range(start, end) as a list column and explode. The number of days n gives base = cents // n and rem = cents % n; the first `...