Flag Daily Invoice Volume Spikes Against a Trailing Baseline
Last updated: November 29, 2026
Quick Overview
Given an invoices table, write SQL that computes each company's daily invoice count including days with zero invoices, compares it with the average of the previous 28 days, and flags days that are more than three times the baseline and at least 20 invoices.
Intuit
November 29, 20260
0
0 solved
Given an invoices table, write SQL that computes each company's daily invoice count including days with zero invoices, compares it with the average of the previous 28 days, and flags days that are more than three times the baseline and at least 20 invoices.
Sudden spikes in invoices sent from a QuickBooks account can mean a bulk import, a seasonal business, or abuse such as invoice spam, and Intuit's risk and data teams monitor this. The interviewer checks that you densify missing days before windowing, exclude the current day from its own baseline, and guard against tiny baselines.
What the Interviewer Expects
- Generate a calendar per company so zero days count in the baseline
- Use a window that excludes the current row from the trailing average
- Require a minimum history before flagging
- Combine a ratio threshold with an absolute floor
- Discuss seasonality and day-of-week effects
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
- Many small businesses only invoice on weekdays or at month end. How would you adjust the baseline?
- How would you implement this as a streaming job that alerts within minutes?
- How would you avoid generating calendar rows for millions of inactive companies?
- Would a median or z-score be more robust here, and why?
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
invoices(invoice_id, company_id, created_at timestamp, status). I count created invoices regardless of status, in UTC days for simplicity. Each company's calendar runs from its first invoice day to ...
Approach
- Aggregate to daily counts.
- Densify with
generate_seriesper company so days with no invoices exist as zeros; otherwise a window of 28 rows could span months and the average would be inflated. ...