purchases has one row per order: user_id, item and purchased_at (a date, as text). A user came back within a week if two of their purchases are 1 to 7 days apart. Two purchases on the same day do not count as coming back. Return a sorted list of those users' ids in result.
The data
purchases15 rows
| user_idint64 | itemobject | purchased_atobject |
|---|
| 1 | Kindle | 2025-03-01 |
| 2 | Cable | 2025-03-01 |
| 3 | Mug | 2025-03-03 |
| 1 | Case | 2025-03-05 |
| 4 | Lamp | 2025-03-03 |
| 2 | Cable | 2025-03-01 |
and 9 more rows
Target output
[1, 3, 5, 7]
What this question tests
Tests gaps between events within a group: sort, take the difference to the previous purchase per user, and filter. It is a retention question in disguise, and the same-day rule is there to see whether you read the definition or pattern-match it.
More Python interview questions