Hone logo
Hone
Problems

User Activity for the Past 30 Days I

This challenge asks you to analyze user activity data to identify active users within a specific recent timeframe. This is crucial for understanding user engagement, tailoring marketing campaigns, and optimizing product features based on recent behavior.

Problem Description

You are given a dataset containing records of user interactions. Each record represents a specific action performed by a user at a given timestamp. Your task is to determine which users have been active in the last 30 days, starting from a given current_date. A user is considered active if they have at least one record within this 30-day period (inclusive of the current_date).

Key Requirements:

  • Identify unique users who have performed any action within the last 30 days.
  • The 30-day period should include the current_date and extend 30 days backward.

Expected Behavior:

The output should be a list of unique user IDs that meet the activity criteria.

Edge Cases:

  • No user activity within the last 30 days.
  • User activity exactly on the current_date.
  • User activity exactly 30 days before the current_date.
  • Empty input data.

Examples

Example 1:

Input:
current_date: "2023-10-26"
activity_logs: [
  {"user_id": "user1", "timestamp": "2023-10-25", "action": "login"},
  {"user_id": "user2", "timestamp": "2023-09-20", "action": "click"},
  {"user_id": "user1", "timestamp": "2023-10-01", "action": "view"},
  {"user_id": "user3", "timestamp": "2023-08-15", "action": "logout"}
]

Output:
["user1"]

Explanation:
The current date is 2023-10-26. The 30-day period extends from 2023-09-27 to 2023-10-26.
- user1 has activity on 2023-10-25 and 2023-10-01. Both are within the 30-day window.
- user2 has activity on 2023-09-20, which is outside the 30-day window.
- user3 has activity on 2023-08-15, which is outside the 30-day window.
Therefore, only user1 is active.

Example 2:

Input:
current_date: "2023-11-15"
activity_logs: [
  {"user_id": "userA", "timestamp": "2023-10-16", "action": "purchase"},
  {"user_id": "userB", "timestamp": "2023-11-15", "action": "comment"},
  {"user_id": "userA", "timestamp": "2023-11-01", "action": "view"}
]

Output:
["userA", "userB"]

Explanation:
The current date is 2023-11-15. The 30-day period extends from 2023-10-17 to 2023-11-15.
- userA has activity on 2023-10-16 (outside window) and 2023-11-01 (inside window).
- userB has activity on 2023-11-15 (inside window).
Both userA and userB are active. The order of the output list does not matter.

Example 3: (Edge Case: No recent activity)

Input:
current_date: "2023-12-01"
activity_logs: [
  {"user_id": "userX", "timestamp": "2023-10-30", "action": "login"},
  {"user_id": "userY", "timestamp": "2023-11-01", "action": "click"}
]

Output:
[]

Explanation:
The current date is 2023-12-01. The 30-day period extends from 2023-11-02 to 2023-12-01.
Both userX and userY have activity outside this window.

Constraints

  • The number of activity logs can range from 0 to 1,000,000.
  • User IDs are strings and can be up to 50 characters long.
  • Timestamps are strings in the format "YYYY-MM-DD".
  • The current_date is a string in the format "YYYY-MM-DD".
  • The solution should be efficient, aiming for a time complexity of O(N) or O(N log N) where N is the number of activity logs.

Notes

  • You will need to implement logic to correctly calculate the date 30 days prior to the current_date.
  • Pay close attention to the inclusivity of the current_date and the 30-day prior date in your comparisons.
  • Consider using a data structure that efficiently stores unique user IDs.
  • The problem statement implies that timestamp and current_date are comparable as dates. You may need to convert these string representations into a format that allows for date arithmetic and comparison.
Loading editor...
plaintext