Compute Median Ad Auction Clearing Price per Hour Without Percentile Functions
Last updated: November 29, 2026
Quick Overview
Given one row per ad auction with a timestamp and clearing price, compute the exact median clearing price for each hour using only window functions, no PERCENTILE_CONT or APPROX_QUANTILES.
0
0
0 solved
Given one row per ad auction with a timestamp and clearing price, compute the exact median clearing price for each hour using only window functions, no PERCENTILE_CONT or APPROX_QUANTILES.
An onsite SQL round question in the style of Google Ads analytics, where the interviewer removes the built-in percentile functions to see if you understand how a median is defined. They check odd and even counts, tie handling and whether your query stays correct when an hour has a single auction.
What the Interviewer Expects
- Compute row position and group size per hour with window functions
- Select the middle row for odd counts and the two middle rows for even counts
- Average the selected rows to get the median
- Use a deterministic tie-breaker in ORDER BY
- Explain cost on billions of rows and when approximate quantiles are acceptable
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 compute the 90th percentile with the same technique?
- On 50 billion auctions a day, what would you use in production and why?
- How do you handle auctions with no winner and a NULL price?
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
auctions(auction_id, auction_ts, clearing_price). Hours are in UTC unless told otherwise. Auctions with no winner have a NULL price, and I will exclude them since they have no clearing price. The ou...
Approach
Number the prices within each hour with ROW_NUMBER() ordered by price, and attach the hour's count with COUNT(*) OVER. For n items the median sits at positions (n+1)/2 and (n+2)/2 using intege...