Calculate rolling 7-day average per user
Quick Overview
Write a query to compute rolling 7-day average grouped by date, handling edge cases like nulls and duplicates.
DE Shaw
Data Manipulation (SQL/Python)
Data Scientist
DE Shaw
Data Scientist
Onsite
Data Manipulation (SQL/Python)
Medium
6
Write a query to compute rolling 7-day average grouped by date, handling edge cases like nulls and duplicates.
This question from DE Shaw's Onsite tests practical data skills. The interviewer wants to see clean, efficient queries that handle edge cases like NULLs, duplicates, and large datasets.
What the Interviewer Expects
- Use advanced SQL features: window functions, CTEs, subqueries
- Write efficient queries that avoid common performance pitfalls
- Handle complex data transformations with multiple joins and aggregations
- Discuss indexing strategy and query optimization
- Address data quality issues: duplicates, missing values, outliers
Key Topics to Cover
Index optimization and query performance
Date/time manipulation
NULL handling and COALESCE
Subqueries and correlated subqueries
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
- What would you do if this query needs to run every 5 minutes?
- How would you validate the correctness of your query results?
- How would you handle slowly changing dimensions in this scenario?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice SQL ProblemsSample Answer
Problem Understanding
The goal is to compute a rolling 7-day average of a metric (e.g., user activity) for each user, grouped by date. The data involved typically resides in a table with at least the following columns: `us...
Approach
- Select Relevant Data: Start by selecting the necessary columns:
user_id,activity_date, andactivity_value. - Handle NULLs: Use COALESCE to replace NULL values in
activity_valuewi...
Submit Your Answer
Markdown supported