I knew it was a gaps-and-islands type problem the second I saw it, which helped.
Start by deduplicating orders to one row per user per calendar day, then use a date-grouping trick (subtract a row number from the order date) to assign a constant group key to consecutive days. Finally, count the size of each group per user and take the maximum to get the longest streak.
Pro tip: Mention that this pattern (date minus row number) is a classic gaps-and-islands technique, and note that it works because consecutive dates increment by 1 while the row number also increments by 1, keeping the difference constant. Also, clarify how you'd handle time zones or partial days if the data has timestamps.
Collapse multiple orders per user per day into a single distinct (user_id, order_date) row, since the streak only cares about whether at least one order occurred on a calendar day.
For each user, compute a group identifier as order_date minus a sequential row number (ordered by date). Consecutive dates will share the same group key.
Group by user_id and the group key, then count the number of days in each group. Each count represents the length of a consecutive-day streak.
For each user, take the maximum count from step 3 to get their longest streak. Optionally, also return the start and end dates of that streak.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.