Find Active Users - Problem

Find Active Users

You're working for an e-commerce platform that needs to identify active customers for targeted marketing campaigns. An active user is someone who has made multiple purchases within a short time window - specifically, a user who has made a second purchase within 7 days of any other purchase.

Given: A database table Users containing purchase records:

Column NameType
user_idint
itemvarchar
created_atdatetime
amountint

Goal: Return a list of user_ids who qualify as active users.

Important: "Within 7 days" means inclusive. For example, if one purchase is on May 31, 2023, then any purchase between May 31, 2023 and June 7, 2023 (inclusive) counts as within 7 days.

Example: If a user bought an item on June 1st and another item on June 6th, they're active (5 days apart). If they bought on June 1st and June 8th, they're still active (7 days apart, inclusive).

Input & Output

Basic Example - Mixed Active and Inactive Users
$ Input: Users = [[1, 'laptop', '2023-05-01', 1000], [1, 'mouse', '2023-05-03', 50], [2, 'book', '2023-06-01', 20], [2, 'pen', '2023-06-15', 5], [3, 'phone', '2023-07-01', 800], [3, 'case', '2023-07-08', 25]]
โ€บ Output: [1, 3]
๐Ÿ’ก Note: User 1: purchased on May 1 and May 3 (2 days apart โ‰ค 7) โ†’ Active. User 2: purchased on Jun 1 and Jun 15 (14 days apart > 7) โ†’ Not active. User 3: purchased on Jul 1 and Jul 8 (7 days apart โ‰ค 7) โ†’ Active.
Edge Case - Exact 7-Day Boundary
$ Input: Users = [[1, 'item1', '2023-05-01', 100], [1, 'item2', '2023-05-08', 200], [2, 'item3', '2023-06-01', 150], [2, 'item4', '2023-06-09', 75]]
โ€บ Output: [1]
๐Ÿ’ก Note: User 1: May 1 to May 8 is exactly 7 days (inclusive) โ†’ Active. User 2: Jun 1 to Jun 9 is 8 days > 7 โ†’ Not active. This tests the inclusive boundary condition.
Single Purchase Users
$ Input: Users = [[1, 'book', '2023-05-01', 25], [2, 'pen', '2023-06-01', 10], [3, 'notebook', '2023-07-01', 15]]
โ€บ Output: []
๐Ÿ’ก Note: All users have only one purchase each, so none can be considered active (need at least 2 purchases within 7 days). Returns empty list.

Constraints

  • 1 โ‰ค number of records โ‰ค 104
  • 1 โ‰ค user_id โ‰ค 103
  • created_at is in 'YYYY-MM-DD' format
  • 1 โ‰ค amount โ‰ค 104
  • item is a non-empty string with length โ‰ค 50

Visualization

Tap to expand
โ˜• Coffee ShopCustomer AMay 1May 43 daysCustomer BJun 1Jun 1514 daysLOYAL CUSTOMERREGULAR CUSTOMER
Understanding the Visualization
1
Collect Visit Data
Gather all customer visit records with timestamps
2
Organize by Customer
Group visits by customer ID and sort chronologically
3
Check Return Visits
For each customer, see if any two visits are within 7 days
4
Mark Loyal Customers
Customers with quick return visits get loyalty status
Key Takeaway
๐ŸŽฏ Key Insight: By sorting dates first, we only need to check consecutive visits rather than all possible pairs, making the solution much more efficient!
Asked in
Amazon 45 Google 38 Meta 32 Microsoft 28
47.3K Views
Medium Frequency
~18 min Avg. Time
1.5K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen