Determine Maximum Consecutive Order Days Per User
orders
+----+---------+------------+
| id | user_id | order_date |
+----+---------+------------+
| 1 | 101 | 2024-01-01 |
| 2 | 101 | 2024-01-02 |
| 3 | 101 | 2024-01-05 |
| 4 | 102 | 2024-01-03 |
| 5 | 102 | 2024-01-04 |
+----+---------+------------+
Scenario
The commerce team wants to know each customer’s best ordering streak for loyalty analysis.
Question
For every user, return the maximum number of consecutive calendar days on which they placed at least one order.
Hints
Generate dense date series per user; use gaps-and-islands or window functions.
Constraints & Assumptions
-
Preserve the scope, facts, inputs, and requested outputs from the prompt above.
-
If the prompt leaves a detail unspecified, state a reasonable assumption before relying on it.
-
Keep the answer interview-ready: concise enough to present, but concrete enough to implement or evaluate.
Clarifying Questions to Ask
-
Clarify SQL dialect or Python library versions, date/time semantics, duplicate handling, and null handling.
-
Define the grain of each intermediate result before aggregating.
-
State expected output columns and ordering explicitly.
What a Strong Answer Covers
-
A query or pandas plan that matches the requested output grain.
-
Correct joins, filters, grouping, window functions, and treatment of NULLs or duplicates.
-
A brief explanation of why the result is correct and how it handles edge cases.
-
Performance notes, indexes/partitioning, and validation queries when relevant.
Follow-up Questions
-
How would you test the query on a tiny hand-built dataset?
-
What changes if duplicate events or late-arriving data are present?
-
Which indexes, clustering, or partitions would help at production scale?