Finding the Users Active Minutes - Problem

Imagine you're a data analyst at a coding platform tracking user engagement patterns. You have access to user activity logs and need to understand how many users are active for specific durations.

Given a 2D integer array logs where each logs[i] = [IDi, timei] represents that user IDi performed an action at minute timei, and an integer k, your task is to analyze user engagement patterns.

Key Points:

  • Multiple users can be active simultaneously
  • A user can perform multiple actions in the same minute (but it counts as 1 active minute)
  • User Active Minutes (UAM) = number of unique minutes a user was active

Return a 1-indexed array of size k where answer[j] represents the number of users whose UAM equals j.

Example: If 3 users were active for exactly 2 minutes each, then answer[2] = 3.

Input & Output

example_1.py โ€” Basic Case
$ Input: logs = [[1,1],[2,2],[1,3],[3,1]], k = 4
โ€บ Output: [2,1,0,0]
๐Ÿ’ก Note: User 1: active at minutes 1 and 3 โ†’ UAM = 2. User 2: active at minute 2 โ†’ UAM = 1. User 3: active at minute 1 โ†’ UAM = 1. So we have 2 users with UAM=1, 1 user with UAM=2, and 0 users with UAM=3 or UAM=4.
example_2.py โ€” Duplicate Activities
$ Input: logs = [[0,5],[1,2],[0,2],[0,5],[1,3]], k = 5
โ€บ Output: [0,2,0,0,0]
๐Ÿ’ก Note: User 0: active at minutes 2 and 5 (minute 5 counted once) โ†’ UAM = 2. User 1: active at minutes 2 and 3 โ†’ UAM = 2. Both users have UAM=2, so answer[1] = 2.
example_3.py โ€” Single User Multiple Times
$ Input: logs = [[1,1],[1,1],[1,1]], k = 1
โ€บ Output: [1]
๐Ÿ’ก Note: User 1 performs multiple actions at minute 1, but it only counts as 1 unique active minute. So UAM = 1, and answer[0] = 1.

Constraints

  • 1 โ‰ค logs.length โ‰ค 104
  • 0 โ‰ค IDi โ‰ค 109
  • 1 โ‰ค timei โ‰ค 105
  • 1 โ‰ค k โ‰ค 1000
  • logs[i].length == 2

Visualization

Tap to expand
โ˜• Coffee Shop Visit TrackingVisit Logs: Customer visits at different hoursCustomer 1 visits at hours 9, 11. Customer 2 visits at hour 10. Customer 3 visits at hour 9.Customer1Hours:{9, 11}Customer2Hours:{10}Customer3Hours:{9}๐Ÿ“Š Engagement Report1-hour visits: 2 customers (Customer 2, Customer 3)2-hour visits: 1 customer (Customer 1)Result: [2, 1, 0, 0] for k=4
Understanding the Visualization
1
Record Visits
Track each customer's unique visit hours using a hash map
2
Count Patterns
Group customers by their total unique visit hours
3
Generate Report
Create frequency report showing engagement distribution
Key Takeaway
๐ŸŽฏ Key Insight: Hash tables efficiently deduplicate activities per user and enable fast frequency counting for engagement analysis
Asked in
Amazon 45 Google 38 Meta 32 Microsoft 28
34.8K Views
Medium Frequency
~15 min Avg. Time
1.2K 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