Similar Problems
Similar Problems not available
Active Users - Leetcode Solution
Companies:
LeetCode: Active Users Leetcode Solution
Difficulty: Medium
Topics: database
Problem Description:
The problem "Active Users" on LeetCode was a question asked in the Weekly Contest 201 on LeetCode. The task was to calculate the number of active users on a particular day, given a log of users' activity on different days. The input is in the form of a list of log entries, where each entry has the following format:
[username, date, action]
The action field can take one of the following four values:
"NewUser" - indicates that the user with the given username has registered on that day. "Logout" - indicates that the user with the given username has logged out on that day. "Login" - indicates that the user with the given username has logged in on that day. "Purchase" - indicates that the user with the given username has made a purchase on that day.
The task was to determine the number of active users on a given day, where an active user is defined as a user who has logged in at any point during the day and has not logged out before the end of the day.
Solution:
To solve this problem, we can use a hashmap to keep track of the activity of each user. We can iterate through each log entry in the input list and update the hashmap accordingly. We can also keep track of the active users for each day in a separate hashmap.
The algorithm for this solution can be broken down into the following steps:
-
Initialize a hashmap to keep track of the activity of each user. The keys of the hashmap will be user names and the values will be lists of tuples representing the user's activity. Each tuple will have the format (date, action).
-
Initialize a hashmap to keep track of the active users for each day. The keys of the hashmap will be dates and the values will be lists of active users.
-
Iterate through each log entry in the input list. For each entry, update the hashmap of activity for the corresponding user.
-
If the action is "Login", add the user to the list of active users for that day.
-
If the action is "Logout", remove the user from the list of active users for that day.
-
Iterate through the list of active users for each day and count the number of active users.
-
Return the count of active users for the given day.
Here is the Python implementation of the above algorithm:
def activeUsers(logs, date): userActivity = {} # Step 1: Initialize a hashmap to keep track of the activity of each user activeUsers = {} # Step 2: Initialize a hashmap to keep track of the active users for each day
# Step 3: Iterate through the list of log entries and update the user activity hashmap
for log in logs:
user, logDate, action = log
if user not in userActivity:
userActivity[user] = []
userActivity[user].append((logDate, action))
# Step 4: If the action is "Login", add the user to the list of active users for that day
if action == "Login":
if logDate not in activeUsers:
activeUsers[logDate] = set()
activeUsers[logDate].add(user)
# Step 5: If the action is "Logout", remove the user from the list of active users for that day
elif action == "Logout":
if logDate in activeUsers and user in activeUsers[logDate]:
activeUsers[logDate].remove(user)
# Step 6: Iterate through the list of active users for the given day and count the number of active users
if date in activeUsers:
return len(activeUsers[date])
else:
return 0
To test the solution, we can use the following sample input:
logs = [["Alice", "2020-01-01", "NewUser"], ["Bob", "2020-01-01", "NewUser"], ["Alice", "2020-01-01", "Login"], ["Bob", "2020-01-01", "Login"], ["Alice", "2020-01-01", "Logout"], ["Bob", "2020-01-01", "Logout"], ["Charlie", "2020-01-01", "NewUser"], ["Charlie", "2020-01-01", "Login"], ["Charlie", "2020-01-02", "Purchase"]]
print(activeUsers(logs, "2020-01-01")) # Output: 1
In this example, there are three users - Alice, Bob, and Charlie. Alice and Bob both logged in on January 1 and logged out later the same day. Charlie logged in on January 1 and made a purchase on January 2. The output of the program correctly indicates that there was only 1 active user on January 1.
Active Users Solution Code
1