Python Interview Prep/Back Within a Week
Amazon logo

Back Within a Week

Amazon·medium·pandas·About 20 min in the room

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_idint64itemobjectpurchased_atobject
1Kindle2025-03-01
2Cable2025-03-01
3Mug2025-03-03
1Case2025-03-05
4Lamp2025-03-03
2Cable2025-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

  • Weeks at Number One · Spotify · easy
  • The Salary Gap · LinkedIn · easy
  • Month-over-Month Revenue · Amazon · medium
  • Average Session Length · Meta · medium
Python
Shift+EnterPython is starting in your browser...
Output

Starting Python...