InterviewStack.io LogoInterviewStack.io

Advanced SQL: Metric Monitoring, Anomaly Detection, and Data Correctness at Scale Questions

SQL for monitoring a business or operational metric over time and catching when it is wrong or behaving abnormally, as distinct from the mechanics of the SQL constructs used to compute it (window function syntax, frame semantics, CTEs, and subqueries are covered by the companion topic Advanced SQL: Window Functions, CTEs, and Subqueries), from defining and computing cohort retention, funnel conversion, DAU/WAU/MAU, and lifetime value themselves (covered by the companion topics User Retention & Engagement, Conversion Funnel Optimization, and SQL for Data Analysis), and from sessionization via inactivity-gap detection (also covered by the companion window-functions topic). Covers rolling-window anomaly and change-point detection (z-score and threshold-based, including seasonality-aware baselines), testing whether a period-over-period change in a rate metric is statistically significant versus noise, decomposing a metric's change into which segments drove it, average revenue per user (ARPU) at both a simple and a rolling/at-scale altitude, keeping cumulative and rolling metrics correct against missing dates, late-arriving or out-of-order events, and incremental (not full-recompute) materialization at very large scale, approximate-distinct (HyperLogLog) computation, standalone timezone/daylight-saving-aware day bucketing, Kaplan-Meier-style survival curves computed in pure SQL, and multi-state subscription churn and retention (renewals, expansions, downgrades, and pauses at the plan level, not just customer presence/absence).

HardTechnical
85 practiced

COUNT(DISTINCT) over a window is unsupported or too slow for your dataset. Design a batch architecture that computes a daily cumulative unique-entity count (for example, unique users per product) across several years of history with incremental updates. Cover storage format, how you maintain and merge approximate-distinct sketches (e.g. HyperLogLog), your backfill approach, and how you would still produce exact counts for small cohorts where the approximation error matters.

MediumTechnical
87 practiced

When computing cumulative or rolling metrics on a time series, missing dates and NULL values can silently distort the result. Describe strategies, and provide SQL, to: (1) generate a complete date series for the reporting range, (2) backfill or forward-fill missing values appropriately, and (3) use last-known-value carry-forward with window functions where that is the correct semantics.

MediumTechnical
70 practiced

A total metric changed between two periods. Given per-segment values for both periods, write SQL to compute each segment's absolute and relative contribution to the overall change, and return the top 3 segments that drove it. Discuss what you would need to normalize for (for example, the overall segment population changing) before concluding a segment is a genuine driver rather than an artifact of its own size shifting.

HardTechnical
68 practiced

Given orders(order_id, seller_id, status, created_at, canceled_at, amount), compute each seller's cancellation rate over the trailing 90 days, excluding sellers with fewer than 50 orders in that window. Then describe the SQL approach for surfacing which sellers have a week-over-week change in cancellation rate large enough to be statistically significant rather than noise.

HardTechnical
76 practiced

Events feeding a rolling or cumulative metric (for example a rolling active-count or a cohort-style metric) can arrive up to two weeks late, and out of order across regions. Design a robust approach to keep the metric correct: describe watermarking and backfill windows, write SQL or pseudocode for idempotent writes, and explain how you would schedule re-computation or incremental updates. Discuss the trade-off between how quickly the metric is available and how accurate it is once all late data has landed.

Unlock Full Question Bank

Get access to all 18 Advanced SQL: Metric Monitoring, Anomaly Detection, and Data Correctness at Scale interview questions and detailed answers.

Sign in to Continue

Join thousands of developers preparing for their dream job.