Flag Anomalous Hourly Google Search Traffic With Seasonal Z-Scores

Last updated: November 29, 2026

Quick Overview

Given hourly query counts per country, flag hours whose traffic deviates strongly from the same hour of the week in the previous four weeks, using a z-score with a floor on the standard deviation.

Google
Data Manipulation (SQL/Python)
Data Scientist
Google
November 29, 2026
Data Scientist
Onsite
Data Manipulation (SQL/Python)
Hard

0

0

0 solved


Given hourly query counts per country, flag hours whose traffic deviates strongly from the same hour of the week in the previous four weeks, using a z-score with a floor on the standard deviation.

Search traffic has strong daily and weekly cycles, so a naive rolling mean flags every morning ramp. This onsite pandas question checks seasonal baselines built with shifts, excluding the current point from its own baseline, densifying missing hours, and guarding against tiny standard deviations.

What the Interviewer Expects
  • Densify hourly series so shifts move by exact hours
  • Use same hour-of-week lags (168 hours) as the baseline
  • Exclude the current value from its own baseline
  • Require full history before scoring
  • Floor the standard deviation to avoid huge z on stable series
  • Discuss thresholds, alert fatigue and holidays
Key Topics to Cover
anomaly detection
seasonality
z-score
pandas shift
time series
How to Approach This
  1. Clarify the schema and expected output format before writing queries.
  2. Use CTEs (WITH clauses) to break complex queries into readable steps.
  3. Consider window functions (ROW_NUMBER, RANK, LAG, LEAD) for ranking and sequential analysis.
  4. Watch for NULLs, duplicates, and edge cases in JOINs and GROUP BY.
  5. For pandas, prefer vectorized operations over row-by-row iteration.
Possible Follow-up Questions
  • How would you handle a holiday that shifts weekly patterns?
  • An outage last week now sits in the baseline. How do you keep it from masking the next one?
  • How would you scale this to 200 countries and 1000 query categories?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice SQL Problems
Sample Answer
Clarify the Data

counts(country, hour, queries). Hours with no row mean zero traffic (or a logging gap, which is exactly what we want to catch). I compare each hour with the same hour-of-week in the previous four we...

Approach

Per country, reindex to a full hourly range filling zeros, so shift(168 * k) lands exactly k weeks back. Concatenate the four lags, take mean and standard deviation across them (skipna=False so in...


Submit Your Answer
Markdown supported